1.0.0 • Published 1 year ago

@huolala-tech/custom-error v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

CustomError · LICENSE

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]