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
merged 1 commit into from Aug 7, 2018

Conversation

@TimJohns
Copy link
Contributor

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

axios.get('/somepaththatdoesnotexist')
  .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 127.0.0.1:80",
  "name": "Error",
  "stack": "Error: connect ECONNREFUSED 127.0.0.1:80\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": [
      null
    ],
    "transformResponse": [
      null
    ],
    "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

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@kevinansfield kevinansfield referenced this pull request Feb 8, 2019

Open

Expose primitives in GhostAdminAPI #48

1 of 5 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.