Javascript console.log errors: how to see the real object of the Error

Viewed 1275

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})
  })

Firefox Chrome

Update after approved answer. Now I got the full error object.

enter image description here

1 Answers

Simple 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.

Related