Enhance Axios request errors using VError.
npm install @centrapay/axios-verror
Handle axios errors with AxiosVError.enhance
:
axios.request(args).catch(AxiosVError.enhance);
Error will be rethrown as a VError with a better error message and
with VError.info()
populated with structured details about the
error.
const AxiosVError = require('@centrapay/axios-verror');
const axios = require('axios');
async function callApi(data) {
await axios.request({
url: 'http://myhost/',
method: 'POST',
data,
}).catch(AxiosVError.enhance);
}
try {
await callApi({ msg: 'hello' });
}
catch (e) {
if (VError.info(e).axios?.status == 421) {
// handle rate limit
}
}
Structured details about the error can be extracted with
VError.info(err).axios
. An object with the following properties will be
provided:
Name | Type | Description |
---|---|---|
method | String | HTTP request method (GET, POST etc). |
url | String | HTTP request URL. Query params not included when defined as Axios "params". |
status | Number (Optional) | HTTP response status code. |
message | String (Optional) | Extracted HTTP response message. See extractMessage() option to customize. |
res | Object (Optional) | Raw Axios response object. See Axios Response Schema. |
Only the requested method and URL are guaranteed to be present. Other properties require a response. Whether a response is available depends on the underlying error. For example, if there is a network connectivity issue then there will not be an HTTP response available.
Axios error messages normally only provide an HTTP status code. This, presumably, prevents Axios leaking potentially sensitive details into logs, such as host names or path parameters, but it makes debugging errors difficult. Example Axios error message:
Request failed with status code 400
Enhanced error messages include status, method, URL and a formatted message extracted from the response. Example enhanced message:
[400] POST https://host/ (invalid request): Request failed with status code 400
Create a modified instance of AxiosVError with AxiosVError.configure()
:
const axiosVError = AxiosVError.configure({
extractMessage: res => res?.data?.error_code
});
axios.request(args).catch(axiosVError.enhance);
AxiosVError instances can be reused.
The following options are available for configuring AxiosVError behavior.
A function which extracts the error message from the axios response. By
default, the message will be attempted to be taken from the parsed Axios
response body as either res.data.message
or res.data.error.message
.
A function which generates the formatted error message. By default, the message will be formatted like:
`[${status}] ${method} ${url} (${message})`
See Changelog
Copyright © 2022 Centrapay.
This software is licensed under Apache-2.0 License. Please see LICENSE for details.