1class CustomError extends Error {
2 constructor(foo = 'bar', ...params) {
3 // Pass remaining arguments (including vendor specific ones) to parent constructor
4 super(...params)
5
6 // Maintains proper stack trace for where our error was thrown (only available on V8)
7 if (Error.captureStackTrace) {
8 Error.captureStackTrace(this, CustomError)
9 }
10
11 this.name = 'CustomError'
12 // Custom debugging information
13 this.foo = foo
14 this.date = new Date()
15 }
16}
17
18try {
19 throw new CustomError('baz', 'bazMessage')
20} catch(e) {
21 console.error(e.name) //CustomError
22 console.error(e.foo) //baz
23 console.error(e.message) //bazMessage
24 console.error(e.stack) //stacktrace
25}
1class Testing extends Error {
2 constructor(message) {
3 super(message);
4 this.name = this.constructor.name;
5 this.message = message;
6 Error.captureStackTrace(this, this.constructor);
7 }
8}