github-pages-deploy-action/node_modules/make-error/README.md

112 lines
2.2 KiB
Markdown
Raw Normal View History

2020-03-31 20:57:48 +08:00
# make-error [![Build Status](https://img.shields.io/travis/JsCommunity/make-error/master.svg)](http://travis-ci.org/JsCommunity/make-error)
2020-03-31 20:42:07 +08:00
> Make your own error types!
2020-03-31 20:57:48 +08:00
2020-03-31 20:42:07 +08:00
## Features
- Compatible Node & browsers
- `instanceof` support
- `error.name` & `error.stack` support
- compatible with [CSP](https://en.wikipedia.org/wiki/Content_Security_Policy) (i.e. no `eval()`)
## Installation
### Node & [Browserify](http://browserify.org/)/[Webpack](https://webpack.js.org/)
Installation of the [npm package](https://npmjs.org/package/make-error):
```
> npm install --save make-error
```
Then require the package:
```javascript
2020-03-31 20:57:48 +08:00
var makeError = require('make-error');
2020-03-31 20:42:07 +08:00
```
### Browser
You can directly use the build provided at [unpkg.com](https://unpkg.com):
```html
<script src="https://unpkg.com/make-error@1/dist/make-error.js"></script>
```
## Usage
### Basic named error
```javascript
2020-03-31 20:57:48 +08:00
var CustomError = makeError('CustomError')
2020-03-31 20:42:07 +08:00
// Parameters are forwarded to the super class (here Error).
2020-03-31 20:57:48 +08:00
throw new CustomError('a message')
2020-03-31 20:42:07 +08:00
```
### Advanced error class
```javascript
2020-03-31 20:57:48 +08:00
function CustomError (customValue) {
CustomError.super.call(this, 'custom error message')
2020-03-31 20:42:07 +08:00
2020-03-31 20:57:48 +08:00
this.customValue = customValue
2020-03-31 20:42:07 +08:00
}
2020-03-31 20:57:48 +08:00
makeError(CustomError)
2020-03-31 20:42:07 +08:00
// Feel free to extend the prototype.
2020-03-31 20:57:48 +08:00
CustomError.prototype.myMethod = function CustomError$myMethod () {
console.log('CustomError.myMethod (%s, %s)', this.code, this.message)
}
2020-03-31 20:42:07 +08:00
//-----
try {
2020-03-31 20:57:48 +08:00
throw new CustomError(42)
2020-03-31 20:42:07 +08:00
} catch (error) {
2020-03-31 20:57:48 +08:00
error.myMethod()
2020-03-31 20:42:07 +08:00
}
```
### Specialized error
```javascript
2020-03-31 20:57:48 +08:00
var SpecializedError = makeError('SpecializedError', CustomError);
2020-03-31 20:42:07 +08:00
throw new SpecializedError(42);
```
### Inheritance
> Best for ES2015+.
```javascript
2020-03-31 20:57:48 +08:00
import {BaseError} from 'make-error'
2020-03-31 20:42:07 +08:00
class CustomError extends BaseError {
2020-03-31 20:57:48 +08:00
constructor () {
super('custom error message')
2020-03-31 20:42:07 +08:00
}
}
```
## Related
- [make-error-cause](https://www.npmjs.com/package/make-error-cause): Make your own error types, with a cause!
## Contributions
2020-03-31 20:57:48 +08:00
Contributions are *very* welcomed, either on the documentation or on
2020-03-31 20:42:07 +08:00
the code.
You may:
- report any [issue](https://github.com/JsCommunity/make-error/issues)
you've encountered;
- fork and create a pull request.
## License
ISC © [Julien Fontanet](http://julien.isonoe.net)