"use strict";
/**
 * 异常模块
 * @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.throwApiErrorFromAxiosError = exports.generateApiErrorFromAxiosError = exports.ApiError = exports.FriendlyError = exports.ParamEmptyError = exports.ParamInvalidError = exports.ParamError = void 0;
/**
 * 参数异常类
 *
 * ```ts
 * import { ParamError } from "refs/nslib";
 *
 * throw new ParamError('id is null.','id');
 * ```
 * @param paramName 参数名
 */
var ParamError = /** @class */ (function (_super) {
    __extends(ParamError, _super);
    function ParamError(message, paramName) {
        var _this = _super.call(this) || this;
        _this.name = _this.constructor.name;
        _this.message = message;
        _this.paramName = paramName;
        return _this;
    }
    return ParamError;
}(Error));
exports.ParamError = ParamError;
/**
 * 参数无效异常类
 *
 * ```ts
 * import { ParamInvalidError } from "refs/nslib";
 *
 * throw new ParamInvalidError('id');
 * ```
 * @param paramName 参数名
 */
var ParamInvalidError = /** @class */ (function (_super) {
    __extends(ParamInvalidError, _super);
    function ParamInvalidError(paramName) {
        var _this = _super.call(this, "invalid " + (paramName || 'param') + ".", paramName) || this;
        _this.name = _this.constructor.name;
        Object.setPrototypeOf(_this, ParamInvalidError.prototype);
        return _this;
    }
    return ParamInvalidError;
}(ParamError));
exports.ParamInvalidError = ParamInvalidError;
/**
 * 参数为空异常类
 * ```ts
 * import { ParamEmptyError } from "refs/nslib";
 *
 * throw new ParamEmptyError('id');
 * ```
 * @param paramName 参数名
 */
var ParamEmptyError = /** @class */ (function (_super) {
    __extends(ParamEmptyError, _super);
    function ParamEmptyError(paramName) {
        var _this = _super.call(this, (paramName || 'param') + " is empty.", paramName) || this;
        _this.name = _this.constructor.name;
        Object.setPrototypeOf(_this, ParamEmptyError.prototype);
        return _this;
    }
    return ParamEmptyError;
}(ParamError));
exports.ParamEmptyError = ParamEmptyError;
var FriendlyError = /** @class */ (function (_super) {
    __extends(FriendlyError, _super);
    function FriendlyError(message) {
        var _this = _super.call(this) || this;
        _this.name = _this.constructor.name;
        _this.message = message;
        Object.setPrototypeOf(_this, FriendlyError.prototype);
        return _this;
    }
    return FriendlyError;
}(Error));
exports.FriendlyError = FriendlyError;
/**
 * API异常类
 *
 * 由于`Axios`的`AxiosError`异常属性比较多, 且在PROD环境下使用多线程记录日志, 使用`postMessage`传输日志信息(`HTML结构化克隆算法`),
 * 只能传输`AxiosError`的`Message`信息, 所以使用`ApiError`代替`AxiosError`
 *
 * ```ts
 * import { throwApiErrorFromAxiosError } from "refs/nslib";
 *
 * const axiosInstance = axios.create({
 *   baseURL: 'xxx'
 * });
 *
 * axiosInstance.interceptors.request.use(config => {
 *   return config;
 * }, error => {
 *   throwApiErrorFromAxiosError(error);
 * });
 *
 * axiosInstance.interceptors.response.use(response => {
 *   return response;
 * }, error => {
 *   throwApiErrorFromAxiosError(error);
 * });
 * ```
 */
var ApiError = /** @class */ (function () {
    function ApiError() {
    }
    ApiError.prototype.toString = function () {
        return "" + (this.apiCategory || '') + (this.apiDesc ? '`' + this.apiDesc + '`' : '') + "\u8C03\u7528\u5931\u8D25,\u9519\u8BEF\u4FE1\u606F:" + this.message + ",\u5730\u5740:" + this.url + ",\u8BF7\u6C42\u5934:" + JSON.stringify(this.headers) + ",\u8BF7\u6C42\u53C2\u6570:" + JSON.stringify(this.data) + (this.plaintext ? ',明文:' + JSON.stringify(this.plaintext) : '') + ",HTTP\u72B6\u6001\u7801:" + this.status + ",\u8FD4\u56DE\u503C:" + JSON.stringify(this.response);
    };
    return ApiError;
}());
exports.ApiError = ApiError;
function generateApiErrorFromAxiosError(axiosError, apiCategory) {
    var _a, _b, _c;
    var apiError = new ApiError();
    if (apiCategory)
        apiError.apiCategory = apiCategory;
    apiError.message = axiosError.message;
    apiError.url = ((_a = axiosError.config.url) === null || _a === void 0 ? void 0 : _a.startsWith('http')) ? axiosError.config.url : (axiosError.config.baseURL + (axiosError.config.url || ''));
    apiError.headers = axiosError.config.headers;
    apiError.data = axiosError.config.data;
    apiError.code = axiosError.code;
    apiError.status = (_b = axiosError.response) === null || _b === void 0 ? void 0 : _b.status;
    apiError.response = (_c = axiosError.response) === null || _c === void 0 ? void 0 : _c.data;
    if (axiosError.config['apiDesc'])
        apiError.apiDesc = axiosError.config['apiDesc'];
    if (axiosError.config['plaintext'])
        apiError.plaintext = axiosError.config['plaintext'];
    return apiError;
}
exports.generateApiErrorFromAxiosError = generateApiErrorFromAxiosError;
/**
 * 从AxiosError抛出ApiError异常
 *
 * @param apiCategory 接口分类
 */
function throwApiErrorFromAxiosError(error, apiCategory) {
    if (error.isAxiosError)
        throw generateApiErrorFromAxiosError(error, apiCategory);
    else
        throw error;
}
exports.throwApiErrorFromAxiosError = throwApiErrorFromAxiosError;
//# sourceMappingURL=Errors.js.map