"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildAxiosError = exports.isAxiosError = exports.buildResponseFromDfnsError = exports.isDfnsError = void 0;
const errors_1 = require("../errors");
const ResponseBuilder_1 = require("../lambdas/ResponseBuilder");
const strings_1 = require("./strings");
const isDfnsError = (error) => error.toErrorObject && typeof error.toErrorObject === 'function';
exports.isDfnsError = isDfnsError;
const buildResponseFromDfnsError = (error, includeStackTrace) => {
    const _a = error.toErrorObject(), { httpStatus } = _a, errorClean = __rest(_a, ["httpStatus"]);
    if (!includeStackTrace)
        delete errorClean.stackTrace;
    return ResponseBuilder_1.ResponseBuilder.error(errorClean, httpStatus);
};
exports.buildResponseFromDfnsError = buildResponseFromDfnsError;
const isAxiosError = (error) => error.isAxiosError || error.request || error.response;
exports.isAxiosError = isAxiosError;
function extractErrorFromAxiosResponse(error) {
    var _a, _b;
    const responseData = (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data;
    if ((0, strings_1.isStringAndNonEmpty)(responseData)) {
        return responseData;
    }
    // avoids multiple nesting of errors if an error occurs somewhere far the lambda callstack
    return ((_b = responseData === null || responseData === void 0 ? void 0 : responseData.error) === null || _b === void 0 ? void 0 : _b.message) || (responseData === null || responseData === void 0 ? void 0 : responseData.message) || (responseData === null || responseData === void 0 ? void 0 : responseData.error) || JSON.stringify(responseData);
}
const buildAxiosError = (error, includeStackTrace) => {
    if (error.response) {
        const errorResponse = (0, exports.buildResponseFromDfnsError)(errors_1.TechnicalError.genericError(error.response.status, extractErrorFromAxiosResponse(error)), includeStackTrace);
        return Object.assign(Object.assign({}, errorResponse), { headers: Object.assign(Object.assign({}, error.response.headers), errorResponse.headers) });
    }
    if (error.request) {
        return (0, exports.buildResponseFromDfnsError)(errors_1.TechnicalError.requestTimeout(), includeStackTrace);
    }
    return (0, exports.buildResponseFromDfnsError)(errors_1.TechnicalError.internalServerError('axios request misbehaved'), includeStackTrace);
};
exports.buildAxiosError = buildAxiosError;
//# sourceMappingURL=http.js.map