When console.logging errors, the browser prints a differently styled output than if it were a normal object. How can you force Google / Firefox to print the real object of the Error class instead of the stylized less useful 'error' output?
I know for example the object contains e.message
and e.response
for example, which you can never deduct from the Browsers log outputs.
api
.post('/post/create', formData)
.then((res) => {
})
.catch((e) => {
// doesn't print the error object
// doesn't print the error object
// doesn't print the error object
console.log(e)
// UPDATE, destructuring does print the full Error object
// UPDATE, destructuring does print the full Error object
// UPDATE, destructuring does print the full Error object
console.log('full error object', {e})
})
Update after approved answer. Now I got the full error object.
When console.logging errors, the browser prints a differently styled output than if it were a normal object. How can you force Google / Firefox to print the real object of the Error class instead of the stylized less useful 'error' output?
I know for example the object contains e.message
and e.response
for example, which you can never deduct from the Browsers log outputs.
api
.post('/post/create', formData)
.then((res) => {
})
.catch((e) => {
// doesn't print the error object
// doesn't print the error object
// doesn't print the error object
console.log(e)
// UPDATE, destructuring does print the full Error object
// UPDATE, destructuring does print the full Error object
// UPDATE, destructuring does print the full Error object
console.log('full error object', {e})
})
Update after approved answer. Now I got the full error object.
Share Improve this question edited Feb 3, 2022 at 9:43 ViBoNaCci asked Feb 3, 2022 at 8:10 ViBoNaCciViBoNaCci 4806 silver badges18 bronze badges 6- Why doesn't the stack trace help? – Salman Arshad Commented Feb 3, 2022 at 8:12
- You made a request. The server responded with 401. So check the request/response in the network tab and/or the server. – Andreas Commented Feb 3, 2022 at 8:14
- And the browser cannot show your server node code – mplungjan Commented Feb 3, 2022 at 8:16
- I just want the underlying error object, which it refuses to print. – ViBoNaCci Commented Feb 3, 2022 at 8:37
- You won't get any relevant information on the client about a problem on the server. If you send information about the problem with the response then you shouldn't use an error response code (4xx, 5xx). – Andreas Commented Feb 3, 2022 at 8:42
1 Answer
Reset to default 14Simple solution - log the error object as a property of a plain object
try {
throw Error("Some error text");
}
catch( error) {
console.log( {error});
}
This allows you to inspect and expand the structure of an error object in the same way you would any other. Best performed in a browser since code snippets don't provide inspection facilities.