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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 tasks
This was referenced Aug 13, 2019
This was referenced Aug 29, 2019
This was referenced Sep 22, 2019
This was referenced Oct 16, 2019
This was referenced Oct 26, 2019
This was referenced Mar 25, 2020
1 task
This was referenced Mar 27, 2020
This was referenced Apr 6, 2020
This was referenced Apr 13, 2020
This was referenced Apr 21, 2020
This was referenced Apr 30, 2020
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 byJSON.stringify()
to successfully serialize the objects.Prior to this change, the following would result in
UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
:After the change, the output is: