|
|
3 months ago | |
|---|---|---|
| .. | ||
| dist | 3 months ago | |
| CHANGELOG.md | 3 months ago | |
| LICENSE | 3 months ago | |
| README.md | 3 months ago | |
| SECURITY.md | 3 months ago | |
| package.json | 3 months ago | |
ts-custom-error is a tiny (~500 bytes of minified & gzipped Javascript) package providing a CustomError class and a customErrorFactory function to easily extends native Error in node and evergreen browsers.
It's written in Typescript and try to offer the best development and debug experiences: bundled in Javascript with Typescript definition files, map files and bundled js files for various environments: transpiled to es5 with commonjs, module and umd exports, the umd bundle is also available minified for easy import in browsers.
Because extending native Error in node and in browsers is tricky
class MyError extends Error {
constructor(m) {
super(m)
}
}
doesn't work as expected in ES6 and is broken in Typescript.
CustomError classSimply extends and call super in you custom constructor.
import { CustomError } from 'ts-custom-error'
class HttpError extends CustomError {
public constructor(
public code: number,
message?: string,
) {
super(message)
}
}
...
new HttpError(404, 'Not found')
You may want more advanced contructor logic and custom methods, see examples
customErrorFactory factoryCustom error contructor returned by the factory pass the same unit tests as Class constructor.
Factory still allows custom logic inside constructor:
import { customErrorFactory } from 'ts-custom-error'
const HttpError = customErrorFactory(function HttpError (code: number, message= '') {
this.code = code
this.message = message
})
...
new HttpError(404, 'Not found')
Custom Error from customErrorFactory can:
ts
HttpError(404, 'Not found')
const ValidationError = customErrorFactory(function ValidationError (message= 'Invalid parameter') { this.message = message }, TypeError)
### Known limitations
#### Minification and transpilation mangle custom Error names.
Unexpected results are:
- Minified identifiers in place of custom Error name in Stacktrace
- Wrong error recognition where using errors name (bad practice) instead of `instanceof`
You may fix this behaviour by:
- Using [uglifyjs options](https://github.com/mishoo/UglifyJS2/blob/harmony/README.md) `--mangle 'except=["MyError"]'` (need to specify all custom error names) or `--keep_fnames` / `--keep_classnames` (nothing to specify but your bundle size will be larger)
- Setting explicitly error name:
```ts
import { CustomError } from 'ts-custom-error'
class MyError extends CustomError {
constructor() {
super()
// Set name explicitly as minification can mangle class names
Object.defineProperty(this, 'name', { value: 'MyError' })
}
}
import { customErrorFactory } from 'ts-custom-error'
const MyError = customErrorFactory(function MyError () {
// Set name explicitly as minification can remove function expression names
Object.defineProperty(this, 'name', { value: 'MyError' })
})
Watch source changes and run corresponding unit tests
npm start
Run all unit tests
npm test
Get coverage report
npm run coverage
Format staged code and run commitizen (enforce commit message convention)
npm run commit
⚠️ This project is mainly a pet project and its first purpose is to be a playground for various external services and tools:
Starting version 3.0.0 this project is under MIT licence, there are no code change between version 2.2.2 and version 3.0.0 but changing licence was considered as a breaking change. All versions < 3.0.0 are under WTFPL.