"use strict";
/**
 * API异常模块
 * @date 2022-03
 * @author 何华东
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.throwApiErrorFromAxiosResponse = exports.throwApiErrorFromAxiosError = exports.ApiError = void 0;
/**
 * 接口异常
 */
var ApiError = /** @class */ (function () {
    function ApiError() {
    }
    ApiError.prototype.toString = function () {
        return this.apiCategory + "'" + (this.apiDesc || '') + "'\u8C03\u7528\u5931\u8D25,\u9519\u8BEF\u4FE1\u606F:" + (this.message || '') + ",\u8BF7\u6C42\u5730\u5740:" + this.requestUrl + ",\u8BF7\u6C42\u5934:" + JSON.stringify(this.requesHeaders) + ",\u8BF7\u6C42\u6570\u636E:" + JSON.stringify(this.requestData) + ",HTTP\u72B6\u6001\u7801:" + this.statusCode + ",\u8FD4\u56DE\u503C:" + JSON.stringify(this.apiResponse);
    };
    return ApiError;
}());
exports.ApiError = ApiError;
/**
 * 从axiosError生成一个ApiError异常对象
 * @param apiCategory api分类名，例如：统一接口、非标接口
 */
function generateApiErrorFromAxiosError(axiosError, apiCategory) {
    var _a, _b, _c;
    var apiError = new ApiError();
    apiError.apiCategory = apiCategory;
    apiError.apiDesc = axiosError.config['apiDesc'];
    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.apiResponse = (_c = axiosError.response) === null || _c === void 0 ? void 0 : _c.data;
    return apiError;
}
/**
 * 从axiosResponse生成一个ApiError异常对象
 * @param apiCategory api分类名，例如：统一接口、非标接口
 */
function generateApiErrorFromAxiosResponse(axiosResponse, apiCategory, message) {
    var _a;
    var apiError = new ApiError();
    apiError.apiCategory = apiCategory;
    apiError.message = message;
    apiError.apiDesc = axiosResponse.config['apiDesc'];
    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.apiResponse = axiosResponse.data;
    return apiError;
}
/**
 * 从AxiosError抛出ApiError异常
 * @param apiCategory 接口分类名
 */
function throwApiErrorFromAxiosError(axiosError, apiCategory) {
    throw generateApiErrorFromAxiosError(axiosError, apiCategory);
}
exports.throwApiErrorFromAxiosError = throwApiErrorFromAxiosError;
/**
 * 从AxiosResponse抛出ApiError异常
 * @param message 错误信息
 * @param apiCategory 接口分类名
 */
function throwApiErrorFromAxiosResponse(axiosResponse, apiCategory, message) {
    throw generateApiErrorFromAxiosResponse(axiosResponse, apiCategory, message);
}
exports.throwApiErrorFromAxiosResponse = throwApiErrorFromAxiosResponse;
//# sourceMappingURL=ApiError.js.map