Linked Questions

0 votes
1 answer
140 views

The `message` field value is not set in `CustomError` class in NodeJS with the classical inheritance through the function-constructor [duplicate]

This case works just fine: class CustomError2015 extends Error { constructor(message) { super(message); // here the message is set correctly console.log("Message: " + this.message)...
Serg's user avatar
  • 7,293
293 votes
25 answers
293k views

How do I create a custom Error in JavaScript?

For some reason it looks like constructor delegation doesn't work in the following snippet: function NotImplementedError() { Error.apply(this, arguments); } NotImplementedError.prototype = new ...
cdleary's user avatar
  • 70.8k
247 votes
13 answers
128k views

Custom exception type

Can I define custom types for user-defined exceptions in JavaScript? If so, how would I do it?
Manki's user avatar
  • 3,869
140 votes
14 answers
72k views

Extending Error in Javascript with ES6 syntax & Babel

I am trying to extend Error with ES6 and Babel. It isn't working out. class MyError extends Error { constructor(m) { super(m); } } var error = new Error("ll"); var myerror = new MyError("ll")...
Karel Bílek's user avatar
  • 37.3k
179 votes
8 answers
144k views

Handling specific errors in JavaScript (think exceptions)

How would you implement different types of errors, so you'd be able to catch specific ones and let others bubble up..? One way to achieve this is to modify the prototype of the Error object: Error....
cllpse's user avatar
  • 21.6k
135 votes
11 answers
28k views

Is it possible to get the non-enumerable inherited property names of an object?

In JavaScript we have a few ways of getting the properties of an object, depending on what we want to get. 1) Object.keys(), which returns all own, enumerable properties of an object, an ECMA5 ...
dkugappi's user avatar
  • 2,754
93 votes
6 answers
52k views

Re-throwing exception in NodeJS and not losing stack trace

How can I rethrow an error or exception in nodejs/javascript and include a custom message. I have the following code var json = JSON.parse(result); and I wanted to include the result content in the ...
alayor's user avatar
  • 4,865
67 votes
4 answers
110k views

How to return from a Promise's catch/then block?

There are many tutorials on how to use "then" and "catch" while programming with JavaScript Promise. However, all these tutorials seem to miss an important point: returning from a then/catch block to ...
lixiang's user avatar
  • 1,981
33 votes
6 answers
12k views

How to specify a "caused by" in a JavaScript Error?

In my NodeJS program, I parse some user JSON file. So I use : this.config = JSON.parse(fs.readFileSync(path)); The problem is that if the json file is not correctly formated, the error thrown is ...
Anthony O.'s user avatar
  • 23.7k
19 votes
3 answers
29k views

How to throw InvalidArgumentException JavaScript?

Objective Throw InvalidArgumentException in a JavaScript method like one does in Java or similar languages. Background I have been trying to familiarize myself with JavaSctipt error handling, and I ...
Flame_Phoenix's user avatar
24 votes
2 answers
9k views

Throwing custom exceptions in Javascript. Which style to use?

Douglas Crockford recommends doing something like this: throw { name: "System Error", message: "Something horrible happened." }; But you could also do something like this: function ...
Vivin Paliath's user avatar
9 votes
3 answers
14k views

Typescript: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.ts(7009)

The following is the code that works best for displaying custom errors in Chrome Devtools, Node.js, etc. Based on this StackOverflow answer. function CustomErr (message) { var err = new Error(...
Ben Gubler's user avatar
  • 1,433
11 votes
2 answers
7k views

Set Error cause in JavaScript (node.js)

is it possible to specify the cause of an Error in JavaScript (node.js)? I found the Mozilla documentation which defines how to set the message / file / line but not the cause of an error. The reason ...
ooxi's user avatar
  • 3,249
4 votes
4 answers
2k views

Correct way to create custom errors in javascript

What is the correct way to define a custom error in JavaScript? Searching through SO I've found about 6 different ways to define a custom error, but I'm unsure as to the (dis)advantages to each of ...
n0w's user avatar
  • 85
6 votes
2 answers
1k views

Expressjs - Handling Errors With Middleware

I'm building an API (using Expressjs v4) and typically I've dealt with errors within the route rather than using middleware. For example: router.use('/', function(req, res, next) { ... if (err) ...
tommyd456's user avatar
  • 10.6k

15 30 50 per page