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

@redtea/format-axios-error

v2.1.1

Published

Pretty format axios error. Can by used with logform and winston.

Downloads

53,621

Readme

@redtea/format-axios-error

This is a small library that provides a function to format an axios error object for logging purposes. It strips any unnecessary information from the error object, making it more readable and easy to understand when reviewing logs.

Can be used with logform and winston.

Installation

Yarn

$ yarn add -E @redtea/format-axios-error

NPM

$ npm install -E @redtea/format-axios-error

Usage

More usage examples can be found in examples directory.

NodeJS

const axios = require('axios');
const {format} = require('@redtea/format-axios-error');

axios
  .get('https://google.com/give-404')
  .catch(error => {
    console.log(
      JSON.stringify(format(error), null, 2)
    )
  });

will print

{
  "name": "Error",
  "isAxiosError": true,
  "config": {
    "url": "https://google.com/give-404",
    "method": "get",
    "headers": [headers],
    "timeout": 0,
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
    "maxContentLength": -1,
    "maxBodyLength": -1
  },
  "response": {
    "data": [data],
    "status": 404,
    "statusText": "Not Found",
    "headers": [headers]
  }
}

In the example, response data and headers are omitted. Run the code snipped locally to see full log output.

How to use with winston and logform

const winston = require('winston');
const axios = require('axios');
const {axiosError} = require('@redtea/format-axios-error/logform');
const {combine, json} = winston.format;	

const logger = winston.createLogger({
  level: 'info',
  format: combine(
    axiosError(),
    json({ space: 2 })
  ),
  transports: [
    new winston.transports.Console(),
  ],
});

axios
  .get('https://google.com/give-404')
  .catch(error => logger.error(error))

will print

{
  "config": {
    "url": "https://google.com/give-404",
    "method": "get",
    "headers": [headers],
    "timeout": 0,
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
    "maxContentLength": -1,
    "maxBodyLength": -1
  },
  "isAxiosError": true,
  "level": "error",
  "response": {
    "status": 404,
    "data": [data],
    "headers": [headers],
    "statusText": "Not Found"
  },
  "message": "Request failed with status code 404"
}

Browser

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>format-axios-error browser example</title>
  <script src="https://unpkg.com/axios" type="application/javascript"></script>
  <script src="https://unpkg.com/@redtea/format-axios-error" type="application/javascript"></script>
</head>
<body>
<script>
  axios
    .get('https://www.googleapis.com/give-404')
    .catch(error => {
      console.log(
        JSON.stringify(
          AxiosErrorFormat.format(error),
          null,
          2
        )
      )
    })
</script>
</body>
</html>