mysterium-vpn-js
Version:
Javascript SDK for Mysterium node
53 lines (52 loc) • 2.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.axiosErrorHandler = void 0;
/**
* Copyright (c) 2022 BlockDev AG
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const api_error_1 = require("../common/api-error");
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
const axiosErrorHandler = (err) => {
var _a, _b, _c, _d, _e, _f;
if (err.response &&
err.response.headers &&
err.response.headers['content-type'] == api_error_1.CONTENT_TYPE_ERR_V1) {
// Server returned 'application/vnd.mysterium.error+json'. Nice.
throw new api_error_1.APIError(err.response.data);
}
// Returned format is not 'application/vnd.mysterium.error+json' -
// Let's do out best to parse the information.
const status = (_a = err.response) === null || _a === void 0 ? void 0 : _a.status;
const path = (_d = (_c = (_b = err.response) === null || _b === void 0 ? void 0 : _b.config) === null || _c === void 0 ? void 0 : _c.url) !== null && _d !== void 0 ? _d : (_e = err.config) === null || _e === void 0 ? void 0 : _e.url;
let message;
if ((_f = err.response) === null || _f === void 0 ? void 0 : _f.data) {
// Response body has content - dump it into the 'message'
message = JSON.stringify(err.response.data);
}
else if (status) {
// No content returned, only HTTP status - use that to generate a message
message = `Request failed with status code ${status}${path ? ' (path="' + path + '")' : ''}`;
}
else if (err.message) {
// Not a server error, probably failed to make a request at all.
// Just assume the standard Error here and use the 'message'.
message = `${err.message}${path ? ' (path="' + path + '")' : ''}`;
}
else {
// Otherwise, a default message.
message = 'Unknown error';
}
const apiErr = {
status,
path,
error: {
code: 'internal',
message,
},
};
throw new api_error_1.APIError(apiErr);
};
exports.axiosErrorHandler = axiosErrorHandler;