JavaScript Error Objects

By

Learn about the JavaScript error objects, Error, AggregateError, RangeError and TypeError, plus message, name, cause, and when each error fires.

~~~

JavaScript gives us several error objects, which are raised in a try/catch expression depending on the error type:

Let’s analyze each one of those.

Error

This is the generic error, and it’s the one all the other error objects inherit from. You will often create one yourself with throw new Error('...'). The more specific types below also inherit from Error.

It contains these common properties:

You can pass cause when you create the error:

try {
  doSomething()
} catch (err) {
  throw new Error('Request failed', { cause: err })
}

Later you read it with error.cause.

It also provides toString(), which is responsible for generating a meaningful string from the error, which can be used to print it to screen.

There is also Error.isError(), a check that works where instanceof Error fails, like with errors created inside an iframe. Support is still limited, so do not rely on it everywhere yet.

AggregateError

An AggregateError represents several errors wrapped as one. You meet it with APIs like Promise.any(), when every promise rejects:

Promise.any([
  Promise.reject(new Error('a')),
  Promise.reject(new Error('b')),
]).catch(err => {
  err instanceof AggregateError //true
  err.errors //[Error: a, Error: b]
})

EvalError

This error is defined in modern JavaScript but never actually thrown by JavaScript, and remains for compatibility purposes. It was defined in ECMAScript 3 but it’s not present in the standard since ECMAScript 5.1.

It was used to indicate that the global function eval() was used incorrectly, in a way incompatible with its definition.

RangeError

A RangeError will fire when a numeric value is not in its range of allowed values.

The simplest example is when you set an array length to a negative value:

[].length = -1 //RangeError: Invalid array length

or when you set it to a number higher than 4294967295

[].length = 4294967295 //4294967295
[].length = 4294967296 //RangeError: Invalid array length

(this magic number is specified in the JavaScript spec as the maximum range of a 32-bit unsigned integer, equivalent to Math.pow(2, 32) - 1)

Here are the most common range errors you can spot in the wild:

ReferenceError

A ReferenceError indicates that an invalid reference value has been detected: a JavaScript program is trying to read a variable that does not exist.

dog //ReferenceError: dog is not defined
dog = 2 //ReferenceError: dog is not defined

Be aware that the above statement will create a dog variable on the global object if not ran in strict mode.

Here are the most common reference errors you can spot in the wild:

SyntaxError

A SyntaxError is raised when a syntax error is found in a program.

Here are some examples of code that generate a syntax error.

A function statement without name:

function() {
  return 'Hi!'
}
//SyntaxError: function statement requires a name

Missing comma after an object property definition:

const dog = {
  name: 'Roger'
  age: 5
}
//SyntaxError: missing } after property list

Here are the most common syntax errors you can spot in the wild:

TypeError

A TypeError happens when a value has a type that’s different than the one expected.

The simplest example is trying to invoke a number:

1() //TypeError: 1 is not a function

Here are the most common type errors you can spot in the wild:

URIError

This error is raised when calling one of the global functions that work with URIs:

and passing an invalid URI.

Want me to talk about your product? You can sponsor this site.

~~~

Related posts about js: