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

GotError.toJSON() #1136

Closed
1 task done
mfulton26 opened this issue Mar 27, 2020 · 4 comments
Closed
1 task done

GotError.toJSON() #1136

mfulton26 opened this issue Mar 27, 2020 · 4 comments
Labels
question The issue is a question about Got

Comments

@mfulton26
Copy link

What problem are you trying to solve?

Easier serialization of errors for logging purposes.

Describe the feature

axios recently implemented toJSON() on AxiosError (axios/axios#1625).

This is super handy when logging errors as AxiosError gets stringified in JSON loggers (e.g. bunyan) to a very sensible subset of all that the error and the request/response include.

Example JSON.stringify(axiosError):
{
  "message": "Request failed with status code 404",
  "name": "Error",
  "stack": "Error: Request failed with status code 404\n    at createError…",
  "config": {
    "url": "https://www.google.com/foo",
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "User-Agent": "axios/0.19.2"
    },
    "transformRequest": [null],
    "transformResponse": [null],
    "timeout": 0,
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
    "maxContentLength": -1,
    "method": "get"
  }
}
Example JSON.stringify(gotError):
{ "name": "HTTPError" }

Checklist

  • I have read the documentation and made sure this feature doesn't already exist.
@mfulton26 mfulton26 changed the title toJSON toJSON() Mar 27, 2020
@mfulton26 mfulton26 changed the title toJSON() GotError.toJSON() Mar 27, 2020
@szmarczak
Copy link
Collaborator

szmarczak commented Mar 27, 2020

Easier serialization of errors for logging purposes.

What information do you expect to serialize? The options property is not enumerable on purpose to prevent the leak of tokens etc.

@szmarczak szmarczak added the question The issue is a question about Got label Mar 27, 2020
@szmarczak
Copy link
Collaborator

Possible duplicate of #559

@szmarczak
Copy link
Collaborator

I think it'd be best to override .toJSON via a beforeError hook.

@szmarczak
Copy link
Collaborator

Duplicate of #1067

@szmarczak szmarczak marked this as a duplicate of #1067 Apr 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question The issue is a question about Got
Projects
None yet
Development

No branches or pull requests

2 participants