npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@centrapay/axios-verror

v1.2.0

Published

Enhance Axios request errors using VError

Downloads

2,579

Readme

Axios Verror

Enhance Axios request errors using VError.

Installation

npm install @centrapay/axios-verror

Usage

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.

Complete Example

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);
}

Handling Enhanced Errors

try {
  await callApi({ msg: 'hello' });
}
catch (e) {
  if (VError.info(e).axios?.status == 421) {
    // handle rate limit
  }
}

VError Info

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.

Error Messages

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

Customizing

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.

Options

The following options are available for configuring AxiosVError behavior.

extractMessage(res)

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.

formatSummary({ status, method, url, message })

A function which generates the formatted error message. By default, the message will be formatted like:

`[${status}] ${method} ${url} (${message})`

History

See Changelog

Legal

Copyright © 2022 Centrapay.

This software is licensed under Apache-2.0 License. Please see LICENSE for details.