Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@redtea/format-axios-error

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@redtea/format-axios-error

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

  • 2.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.8K
decreased by-14.31%
Maintainers
1
Weekly downloads
 
Created
Source

format-axios-error

Format axios error for logs. Support for logform and winston.

Motivation

An axios error contains a lot of information that is not useful in logs. format-axios-error is created to solve that problem. It takes original axios error instance and create new one cutting redundant data.

Installation

Yarn

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

NPM

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

Usage

There are three ways how to use:

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]
  }
}

Response data and headers in this example are omitted. To see full log message run the code snipped locally

Winston and logform

The following example shows how to use the library with winston logger 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>

Keywords

FAQs

Package last updated on 28 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc