Convert an Error Object to a String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
4 min

banner

# Table of Contents

  1. Convert an Error Object to a String in JavaScript
  2. Get the Stack Trace from an Error in JavaScript

# Convert an Error Object to a String in JavaScript

Access the message property on the Error object to convert it to string, e.g. err.message.

The message property is a human-readable description of why the error occurred.

index.js
const err = new Error('Something went wrong'); console.log(err.message); // ๐Ÿ‘‰๏ธ "Something went wrong"

convert error object to string

The code for this article is available on GitHub

When an error is created using the Error() constructor, we can access the message property on the error to get a human-readable string of the reason the error occurred.

In some rare scenarios, third-party packages throw errors without using the native Error() constructor or without extending from it.

# Rejecting a Promise with an Error

The best practice is to always throw an error using the error constructor or extend it if you need added functionality.

index.js
throw new Error('Something went wrong'); Promise.reject(new Error('Something went wrong'));

rejecting promise with an error

The code for this article is available on GitHub

Even when rejecting a promise, you can pass an error to the reject() method.

# Check if the object is an error before accessing message

If you have to deal with strange implementations of an error from third-party packages, you should check if the error value is an object and has the message property to avoid accessing a non-existent property.

index.js
const err = null; if ( typeof err === 'object' && err !== null && 'message' in err ) { const message = err.message; console.log(message); }

check if object is error before accessing message

The code for this article is available on GitHub

Our if condition uses the logical AND (&&) operator, so for the if block to run, all conditions have to be met.

We first check if the err variable stores a value with a type of object because errors have a type of object.

Then we check if the variable is not equal to null.

Unfortunately, if you check the type of null with console.log(typeof null), you will get an "object" value back, so we have to make sure the value is not null.

index.js
console.log(typeof null); // ๐Ÿ‘‰๏ธ "object"
The last thing we check for is that the object contains the message property.

Then we know we can safely access the message property on the object.

# Using error.toString

If that doesn't work, as a last resort, you can try to access the toString() method on the error object.

Some third-party packages throw error objects that implement the toString() method.

index.js
const err = null; if ( typeof err === 'object' && err !== null && 'toString' in err ) { const message = err.toString(); console.log(message); }

using error tostring method

The code for this article is available on GitHub

If that doesn't work either, you have to console.log the error object and investigate what properties and methods it implements.

I've also written an article on how to use and format multiple try-catch blocks in JavaScript.

# Get the Stack Trace from an Error in JavaScript

Use the Error.stack property to get the stack trace from an error.

index.js
function outer() { function inner() { const err = new Error('๐Ÿ‘‰๏ธ Something went wrong'); console.log(err.stack); } inner(); } outer();
The code for this article is available on GitHub

Here is the output of logging the stack property to the console.

error stack property

The stack property of the Error object shows a trace of which functions were called, in what order, in which file and on what line.

There is no standard formatting so the message will look different between different browsers.

The line numbers from the message mean:

  • the stack property on the Error object was logged on line 10 in the index.js file.
  • the error object was created on line 9 in the inner function.
  • the outer function called the inner function on line 13 in the index.js file.
  • the outer function was called on line 16 in the global scope of the index.js file.

# Get the Stack Trace from an Error using console.trace

Alternatively, you can use the console.trace() method.

The console.trace() method outputs the stack trace and shows the call path taken to reach the point at which the method was called.

index.js
function outer() { function inner() { console.trace(); } inner(); } outer();
The code for this article is available on GitHub

We defined a nested function, in which we called the console.trace() method and invoked the outer function.

The method outputs the stack trace to the console if you're in the browser or to the terminal in Node.js.

stack trace output

The message shows the path taken to reach the point at which we called the console.trace() method.

The stack trace from the example shows that the inner function was called from the outer function, which was called from the global scope.

The message also shows the name of the file in which the functions are defined.

The line numbers from the message mean:

  • the console.trace method was invoked in the index.js file on line 9.

  • the inner function called the console.trace method on line 9.

  • the outer function called the inner function in the index.js file on line 12.

  • the outer function was called in the global scope of the index.js file on line 15.

    Which approach you pick is a matter of personal preference. I'd use the console.trace method because it's standardized between browsers.

    If you use the stack property, your results may vary between browsers and some browsers might drop support for it or not implement it as it is a non-standard property.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev