When running this snippet through BabelJS:
class FooError extends Error {
constructor(message) {
super(message);
}
}
let error = new FooError('foo');
console.log(error, error.message, error.stack);
it outputs
{}
which is not what I expect. Running
error = new Error('foo');
console.log(error, error.message, error.stack);
produces
{} foo Error: foo
at eval (eval at <anonymous> (https://babeljs.io/scripts/repl.js?t=2015-05-21T16:46:33+00:00:263:11), <anonymous>:24:9)
at REPL.evaluate (https://babeljs.io/scripts/repl.js?t=2015-05-21T16:46:33+00:00:263:36)
at REPL.compile (https://babeljs.io/scripts/repl.js?t=2015-05-21T16:46:33+00:00:210:12)
at Array.onSourceChange (https://babeljs.io/scripts/repl.js?t=2015-05-21T16:46:33+00:00:288:12)
at u (https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js:28:185)
which is exactly what I would like from the extended error.
My goal is to extend Error
into a variety of subclasses and use them in bluebird's catch
matching. So far, that is failing miserably.
Why is the subclass not showing a message or stack trace?
Edit: using Chrome's built-in subclassing (thanks to @coder) works perfectly. This isn't specific to Babel, necessarily, as the following example (from @loganfsmyth on Babel's gitter feed) shows:
// Works
new (function(){
"use strict";
return class E extends Error { }
}());
// Doesn't
new (function(){
"use strict";
function E(message){
Error.call(this, message);
};
E.prototype = Object.create(Error);
E.prototype.constructor = E;
return E;
}());
Stack trace error is a generic term frequently associated with long error messages. The stack trace information identifies where in the program the error occurs and is helpful to programmers. For users, the long stack track information may not be very useful for troubleshooting web errors.
You can obtain a stack trace from a thread – by calling the getStackTrace method on that Thread instance. This invocation returns an array of StackTraceElement, from which details about stack frames of the thread can be extracted.
What Does Stack Trace Mean? A stack trace is a report that provides information about program subroutines. It is commonly used for certain kinds of debugging, where a stack trace can help software engineers figure out where a problem lies or how various subroutines work together during execution.
In computing, a stack trace (also called stack backtrace or stack traceback) is a report of the active stack frames at a certain point in time during the execution of a program. When a program is run, memory is often dynamically allocated in two places; the stack and the heap.
This is a limitation due to the downlevel compilation of ES6 to ES5. Find more about this in TypeScript's explanation.
As a workaround you can do:
class QueryLimitError extends Error {
__proto__: QueryLimitError;
constructor(message) {
const trueProto = new.target.prototype;
super(message);
this.__proto__ = trueProto;
}
}
In some cases cases Object.setPrototypeOf(this, FooError.prototype);
might be enough.
You'll find more information in the corresponding Github issue and the
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With