"use strict";
/**
 * API异常模块
 * @date 2022-03
 * @author 何华东
 */
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.throwApiErrorFromAxiosResponse = exports.throwApiErrorFromAxiosError = exports.generateApiErrorFromAxiosResponse = exports.generateApiErrorFromAxiosError = void 0;
/**
 * API异常
 */
var ApiError = /** @class */ (function (_super) {
    __extends(ApiError, _super);
    function ApiError() {
        var _this = _super.call(this) || this;
        _this.name = _this.constructor.name;
        return _this;
    }
    return ApiError;
}(Error));
exports.default = ApiError;
/**
 * 从axiosError生成一个ApiError异常对象
 */
function generateApiErrorFromAxiosError(axiosError) {
    var _a, _b, _c;
    var apiError = new ApiError();
    apiError.name = axiosError.name;
    apiError.message = axiosError.message;
    apiError.requestUrl = ((_a = axiosError.config.url) === null || _a === void 0 ? void 0 : _a.startsWith('http')) ? axiosError.config.url : (axiosError.config.baseURL + (axiosError.config.url || ''));
    apiError.requesHeaders = axiosError.config.headers;
    apiError.requestData = axiosError.config.data;
    apiError.code = axiosError.code;
    apiError.statusCode = (_b = axiosError.response) === null || _b === void 0 ? void 0 : _b.status;
    apiError.response = (_c = axiosError.response) === null || _c === void 0 ? void 0 : _c.data;
    return apiError;
}
exports.generateApiErrorFromAxiosError = generateApiErrorFromAxiosError;
/**
 * 从axiosResponse生成一个ApiError异常对象
 * @param message 错误信息
 */
function generateApiErrorFromAxiosResponse(axiosResponse, message) {
    var _a;
    var apiError = new ApiError();
    apiError.message = message;
    apiError.requestUrl = ((_a = axiosResponse.config.url) === null || _a === void 0 ? void 0 : _a.startsWith('http')) ? axiosResponse.config.url : (axiosResponse.config.baseURL + (axiosResponse.config.url || ''));
    apiError.requesHeaders = axiosResponse.config.headers;
    apiError.requestData = axiosResponse.config.data;
    apiError.statusCode = axiosResponse.status;
    apiError.response = axiosResponse.data;
    return apiError;
}
exports.generateApiErrorFromAxiosResponse = generateApiErrorFromAxiosResponse;
/**
 * 从axiosError抛出ApiError异常
 */
function throwApiErrorFromAxiosError(axiosError) {
    throw generateApiErrorFromAxiosError(axiosError);
}
exports.throwApiErrorFromAxiosError = throwApiErrorFromAxiosError;
/**
 * 从axiosResponse抛出ApiError异常
 * @param message 错误信息
 */
function throwApiErrorFromAxiosResponse(axiosResponse, message) {
    throw generateApiErrorFromAxiosResponse(axiosResponse);
}
exports.throwApiErrorFromAxiosResponse = throwApiErrorFromAxiosResponse;
//# sourceMappingURL=ApiError.js.map