@vepler/http-client
Version:
A flexible and extensible API service library for making HTTP requests with built-in authentication support for bearer tokens and API keys.
67 lines • 3.69 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var logger_1 = __importDefault(require("@vepler/logger"));
var http_error_1 = require("../errors/http-error");
var error_utils_1 = require("../errors/error-utils");
/**
* Response error interceptor that converts Axios errors to typed HttpError instances
* and logs them with proper context and sensitive data masking
*/
exports.default = (function (error) {
var _a, _b;
// Create typed, detailed error based on the response
var httpError;
try {
if (error.response) {
// Server responded with an error status (4xx, 5xx)
httpError = (0, http_error_1.createErrorFromResponse)(error.response);
// Log error with contextual information but mask sensitive data
var errorObj = new Error("[HTTP ".concat(httpError.status, "] ").concat(httpError.name, ": ").concat(httpError.message));
logger_1.default.error(errorObj, 'HTTP Response Error', {
status: httpError.status,
endpoint: httpError.endpoint,
errorDetails: (0, error_utils_1.parseAxiosError)(error)
});
}
else if (error.code === 'ECONNABORTED') {
// Timeout error
var _c = error.config || {}, url = _c.url, method = _c.method;
httpError = new http_error_1.TimeoutError("Request timeout after ".concat((_a = error.config) === null || _a === void 0 ? void 0 : _a.timeout, "ms - ").concat(method === null || method === void 0 ? void 0 : method.toUpperCase(), " ").concat(url), error.request, url, method === null || method === void 0 ? void 0 : method.toUpperCase());
var timeoutError = new Error("[TIMEOUT] ".concat(httpError.message));
logger_1.default.error(timeoutError, 'Request Timeout Error', {
timeout: (_b = error.config) === null || _b === void 0 ? void 0 : _b.timeout,
url: url,
method: method
});
}
else if (error.request) {
// Request was made but no response received (network error)
var _d = error.config || {}, url = _d.url, method = _d.method;
httpError = new http_error_1.NetworkError("Network error - ".concat(method === null || method === void 0 ? void 0 : method.toUpperCase(), " ").concat(url), error.request, url, method === null || method === void 0 ? void 0 : method.toUpperCase());
var networkError = new Error("[NETWORK] ".concat(httpError.message));
logger_1.default.error(networkError, 'Network Error', {
url: url,
method: method
});
}
else {
// Error in setting up the request
httpError = new http_error_1.ClientError(error.message || 'Request setup error');
var clientError = new Error("[CLIENT] ".concat(httpError.message));
logger_1.default.error(clientError, 'Client Error');
}
}
catch (loggingError) {
// Fallback in case error parsing/logging fails
var processingError = new Error('Error while processing HTTP error');
logger_1.default.error(processingError, 'Error Processing', { loggingError: loggingError });
// Still create a basic error
httpError = new http_error_1.HttpError(error.message || 'Unknown HTTP error');
}
// Return a rejected promise with our typed error
return Promise.reject(httpError);
});
//# sourceMappingURL=response-error.js.map