#4836·axios

AxiosError's toJSON skips response

Author: KoltesDigitalCreated Jul 8, 2022Updated Jul 2, 2026
Labelstype::breaking

Is your feature request related to a problem? Please describe.

My projects use Axios, and they always do in a way that that promise rejection will end up being logged by Pino, with the error attached. I noticed that the log message contains the request config, but not the response, which would be helpful to fix the error. I believe that error responses (i.e. whose status is not validated) do not contain actual payload that would flood the log, but should only contain details about the error, therefore it's fine to log it. And, for specific projects where that's not the case, users can still remove the response before the error is dumped, using some feature the logging framework should provide (for Pino one would use custom serializers, perhaps formatters or redact too).

Describe the solution you'd like

Add this.response into AxiosError.toJSON.

I could have made a PR, but I feel there is reason why you haven't done it, so this issue is rather a question about that.

Describe alternatives you've considered

Logging takes place at the API entrypoint (e.g. in an Express middleware) and has no access to the Axios request. Therefore one alternative would be to catch every request as your doc mentions and throw a new error including the response. Obviously that would be factorized in a function so that the code overhead would be low, but it'd still be a burden to think about adding it whenever developers write new code.

Additional context

Thanks for your efforts!