"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.ApiError = exports.ValidateSignFailedError = exports.ParamDecryptFailedError = exports.ParamEmptyError = exports.ParamInvalidError = exports.ParamError = void 0;
/**
 * 参数异常类
 * @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;
/**
 * 参数无效异常类
 * @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;
        return _this;
    }
    return ParamInvalidError;
}(ParamError));
exports.ParamInvalidError = ParamInvalidError;
/**
 * 参数为空异常类
 * @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;
        return _this;
    }
    return ParamEmptyError;
}(ParamError));
exports.ParamEmptyError = ParamEmptyError;
/**
 * 参数解密失败异常类
 * @param paramName 参数名
 */
var ParamDecryptFailedError = /** @class */ (function (_super) {
    __extends(ParamDecryptFailedError, _super);
    function ParamDecryptFailedError(paramName) {
        var _this = _super.call(this, (paramName || 'param') + " decrypt failed.", paramName) || this;
        _this.name = _this.constructor.name;
        return _this;
    }
    return ParamDecryptFailedError;
}(ParamError));
exports.ParamDecryptFailedError = ParamDecryptFailedError;
/**
 * 验签失败异常类
 */
var ValidateSignFailedError = /** @class */ (function (_super) {
    __extends(ValidateSignFailedError, _super);
    function ValidateSignFailedError() {
        var _this = _super.call(this, 'validate sign failed.') || this;
        _this.name = _this.constructor.name;
        return _this;
    }
    return ValidateSignFailedError;
}(Error));
exports.ValidateSignFailedError = ValidateSignFailedError;
/**
 * 接口异常类
 *
 * 主要用来替代AxiosError，因为AxiosError属性较多，而且PROD下日志组件只能输出AxiosError的Message属性
 */
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 + ",\u8BF7\u6C42\u5730\u5740:" + this.requestUrl + ",\u8BF7\u6C42\u5934:" + JSON.stringify(this.headers) + ",\u8BF7\u6C42\u6570\u636E:" + JSON.stringify(this.data) + ",HTTP\u72B6\u6001\u7801:" + this.statusCode + ",\u8FD4\u56DE\u503C:" + JSON.stringify(this.apiResponse);
    };
    return ApiError;
}());
exports.ApiError = ApiError;
/**
 * 从axiosError生成一个ApiError异常对象
 */
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.headers = axiosError.config.headers;
    apiError.data = 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;
}
/**
 * 从AxiosError抛出ApiError异常
 * @param apiCategory 接口分类名
 */
function throwApiErrorFromAxiosError(axiosError, apiCategory) {
    throw generateApiErrorFromAxiosError(axiosError, apiCategory);
}
exports.throwApiErrorFromAxiosError = throwApiErrorFromAxiosError;
//# sourceMappingURL=Errors.js.map