Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added toJSON to enhanced Axios errors to facilitate serialization #1625

merged 1 commit into from Aug 7, 2018


Copy link

@TimJohns TimJohns commented Jun 22, 2018

This PR addresses issues #836 and #1301, and possibly #1220 and #460, as well.

Other Error objects in JSON are serializable by default using JSON.stringify(), which is often used for logging and debugging when errors occur, and in some cases for persisting to a db or transmitting over the wire in the form of a crash report. Axios Error objects, however, are not serializable by default due to the circular structures in the request and response.

This is particularly problematic because conditions that result in errors are less likely to occur during the normal course of operation (i.e. when testing the 'happy path'), and so the corresponding code paths are less often tested. When they are tested, they may be tested with mocks that do not include the circular structures, and therefore this behavior is not detected until the corresponding error condition actually occurs in a production environment.

This PR leaves the request and response in place in the JavaScript object, but adds a .toJSON() property to the enhanced error objects, that is used by JSON.stringify() to successfully serialize the objects.

Prior to this change, the following would result in UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON:

const axios = require("axios");

  .catch(function (error) {
    console.log('error:', JSON.stringify(error, null, 2));

After the change, the output is:

bash-3.2$ node index.js 
error: {
  "message": "connect ECONNREFUSED",
  "name": "Error",
  "stack": "Error: connect ECONNREFUSED\n    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1174:14)",
  "config": {
    "url": "/somepaththatdoesnotexist",
    "method": "get",
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "User-Agent": "axios/0.18.0"
    "transformRequest": [
    "transformResponse": [
    "timeout": 0,
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
    "maxContentLength": -1
  "code": "ECONNREFUSED"

@TimJohns TimJohns changed the title Added toJSON to decorated Axios errors to facilitate serialization Added toJSON to enhanced Axios errors to facilitate serialization Jun 22, 2018
@emilyemorehouse emilyemorehouse merged commit 6b44e80 into axios:master Aug 7, 2018
@axios axios locked and limited conversation to collaborators May 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
None yet

Successfully merging this pull request may close these issues.

None yet

2 participants