@huolala-tech/custom-error
v1.0.0
Published
Used to fix the odd behaviors of native Error object inheritance code compiled to ES5
Downloads
139
Keywords
Readme
CustomError ·
Used to fix the odd behaviors of native Error object inheritance code compiled to ES5.
Why?
If your code is compiled to ES5 target, you may encounter an unexpected behavior.
class MyError extends Error {
// blah blah blah
}
const error = new MyError();
console.log(error instanceof Error); // true
console.log(error instanceof MyError); // false <!-- should be true
see https://babeljs.io/docs/en/caveats/#classes
Include
yarn add @huolala-tech/custom-error
or
npm install @huolala-tech/custom-error --save
Use the CustomError
import { CustomError } from '@huolala-tech/custom-error';
class MyError extends CustomError {
// blah blah blah
}
const error = new MyError();
console.log(error instanceof Error); // true
console.log(error instanceof MyError); // true
console.log(error instanceof CustomError); // true
console.log(Object.prototype.toString.call(error)); // [object Error]