Skip to main content
Clarified and simplified text, added caveat, removed self-promotion, return error instead of throw error
Source Link
Jonathan Benn
  • 3.4k
  • 5
  • 25
  • 30

Here is a simple answer that respects this contract:

  1. The stack trace is preserved
  2. The console error message retains the correct custom exception name and error message

Since JavaScript Exceptions are difficult to sub-class, I don't sub-class. I just create a new Exception typeclass and throw a regularuse an Error inside of it. I change the Error.name property so that it looks like my custom exception on the console:

var InvalidInputError = function(message) {
    var error = new Error(message);
    error.name = 'InvalidInputError';
    return error;
};

The above new exception can be thrown just like a regular Error and it will work as expected, for example:

throw new InvalidInputError("Input must be a string");
// Output: Uncaught InvalidInputError: Input must be a string 

Caveat: the stack trace is not perfect, as it will bring you to where the new Error is created and not where you throw. This is not a big deal on Chrome because it provides you with a full stack trace directly in the console. But it's more problematic on Firefox, for example.

Here is a simple answer that respects this contract:

  1. The stack trace is preserved
  2. The console error message retains the correct custom exception name and error message

Since JavaScript Exceptions are difficult to sub-class, I don't sub-class. I just create a new Exception type and throw a regular Error inside of it:

var InvalidInputError = function(message) {
    var error = new Error(message);
    error.name = 'InvalidInputError';
    return error;
};

The above new exception can be thrown just like a regular Error and it will work as expected, for example:

throw new InvalidInputError("Input must be a string");

Caveat: the stack trace is not perfect, as it will bring you to where the new Error is created and not where you throw. This is not a big deal on Chrome because it provides you with a full stack trace. But it's more problematic on Firefox, for example.

Since JavaScript Exceptions are difficult to sub-class, I don't sub-class. I just create a new Exception class and use an Error inside of it. I change the Error.name property so that it looks like my custom exception on the console:

var InvalidInputError = function(message) {
    var error = new Error(message);
    error.name = 'InvalidInputError';
    return error;
};

The above new exception can be thrown just like a regular Error and it will work as expected, for example:

throw new InvalidInputError("Input must be a string");
// Output: Uncaught InvalidInputError: Input must be a string 

Caveat: the stack trace is not perfect, as it will bring you to where the new Error is created and not where you throw. This is not a big deal on Chrome because it provides you with a full stack trace directly in the console. But it's more problematic on Firefox, for example.

Added caveat, removed self-promotion, return error instead of throw error
Source Link
Jonathan Benn
  • 3.4k
  • 5
  • 25
  • 30

I struggled with this issue as well. I found that the other answers here were either too complicated or one of these essential properties ofHere is a custom exception was not being respectedsimple answer that respects this contract:

  1. The stack trace is preserved
  2. The console error message retains the correct custom exception name and error message

The answer I figured out is fully documented in my article, How to Unit Test Customer Exceptions with QUnit.

Here is my answer in a nutshell. Since JavaScript Exceptions are difficult to sub-class, I don't sub-class. I just create a new Exception type and throw a regular Error inside of it:

var InvalidInputError = function(message) {
    var error = new Error(message);
    error.name = 'InvalidInputError';
    throwreturn error;
};

The above new exception can be thrown just like a regular Error and it will work as expected, for example:

throw new InvalidInputError("Input must be a string");

Caveat: the stack trace is not perfect, as it will bring you to where the new Error is created and not where you throw. This is not a big deal on Chrome because it provides you with a full stack trace. But it's more problematic on Firefox, for example.

I struggled with this issue as well. I found that the other answers here were either too complicated or one of these essential properties of a custom exception was not being respected:

  1. The stack trace is preserved
  2. The console error message retains the correct custom exception name and error message

The answer I figured out is fully documented in my article, How to Unit Test Customer Exceptions with QUnit.

Here is my answer in a nutshell. Since JavaScript Exceptions are difficult to sub-class, I don't sub-class. I just create a new Exception type and throw a regular Error inside of it:

var InvalidInputError = function(message) {
    var error = new Error(message);
    error.name = 'InvalidInputError';
    throw error;
};

The above new exception can be thrown just like a regular Error and it will work as expected, for example:

throw new InvalidInputError("Input must be a string");

Here is a simple answer that respects this contract:

  1. The stack trace is preserved
  2. The console error message retains the correct custom exception name and error message

Since JavaScript Exceptions are difficult to sub-class, I don't sub-class. I just create a new Exception type and throw a regular Error inside of it:

var InvalidInputError = function(message) {
    var error = new Error(message);
    error.name = 'InvalidInputError';
    return error;
};

The above new exception can be thrown just like a regular Error and it will work as expected, for example:

throw new InvalidInputError("Input must be a string");

Caveat: the stack trace is not perfect, as it will bring you to where the new Error is created and not where you throw. This is not a big deal on Chrome because it provides you with a full stack trace. But it's more problematic on Firefox, for example.

Removed unnecessary namespace from code
Source Link
Jonathan Benn
  • 3.4k
  • 5
  • 25
  • 30

I struggled with this issue as well. I found that the other answers here were either too complicated or one of these essential properties of a custom exception was not being respected:

  1. The stack trace is preserved
  2. The console error message retains the correct custom exception name and error message

The answer I figured out is fully documented in my article, How to Unit Test Customer Exceptions with QUnit.

Here is my answer in a nutshell. Since JavaScript Exceptions are difficult to sub-class, I don't sub-class. I just create a new Exception type and throw a regular Error inside of it:

namespace.var InvalidInputError = function(message) {
    var error = new Error(message);
    error.name = 'namespace.InvalidInputError';'InvalidInputError';
    throw error;
};

The above new exception can be thrown just like a regular Error and it will work exactly as expected, for example:

throw new namespace.InvalidInputError("Input must be a string");

I struggled with this issue as well. I found that the other answers here were either too complicated or one of these essential properties of a custom exception was not being respected:

  1. The stack trace is preserved
  2. The console error message retains the correct custom exception name and error message

The answer I figured out is fully documented in my article, How to Unit Test Customer Exceptions with QUnit.

Here is my answer in a nutshell. Since JavaScript Exceptions are difficult to sub-class, I don't sub-class. I just create a new Exception type and throw a regular Error inside of it:

namespace.InvalidInputError = function(message) {
    var error = new Error(message);
    error.name = 'namespace.InvalidInputError';
    throw error;
};

The above new exception can be thrown just like a regular Error and it will work exactly as expected, for example:

throw new namespace.InvalidInputError("Input must be a string");

I struggled with this issue as well. I found that the other answers here were either too complicated or one of these essential properties of a custom exception was not being respected:

  1. The stack trace is preserved
  2. The console error message retains the correct custom exception name and error message

The answer I figured out is fully documented in my article, How to Unit Test Customer Exceptions with QUnit.

Here is my answer in a nutshell. Since JavaScript Exceptions are difficult to sub-class, I don't sub-class. I just create a new Exception type and throw a regular Error inside of it:

var InvalidInputError = function(message) {
    var error = new Error(message);
    error.name = 'InvalidInputError';
    throw error;
};

The above new exception can be thrown just like a regular Error and it will work as expected, for example:

throw new InvalidInputError("Input must be a string");
Source Link
Jonathan Benn
  • 3.4k
  • 5
  • 25
  • 30
Loading