Common egg Error Codes
- Dominant language
- TypeScript
- Stars
- 19k
- Forks
- 1.8k
- PR merge metrics
- No merged PRs in 30d
Description
## 前置资料
- 参考 Node.js Error Codes: https://npm.taobao.org/mirrors/node/latest/docs/api/errors.html#errors_node_js_error_codes
- 所有由 egg 和 egg 插件引起已知的 Error 异常,都需要规范 err.code
## RFC:How to create an Error
### Usage
- built-in Error
```js
const errors = require('egg').errors;
const err = new errors.TypeError('ERR_EGG_SOME_ERROR_CODE_STRING', 'Some error haha');
console.log(err.code); // 'ERR_EGG_SOME_ERROR_CODE_STRING'
```
- custom Error
```js
const errors = require('egg').errors;
// register a new subclass of TypeError with code 'ERR_EGG_MY_ERROR'
// if code 'ERR_EGG_MY_ERROR' exists, then will throw a TypeError with code 'ERR_EGG_DUPLICATE_CODE'
errors.E('ERR_EGG_MY_ERROR', TypeError);
const err = new errors.ERR_EGG_MY_ERROR('my error here');
console.log(err.name); // 'TypeError'
console.log(err.code); // 'ERR_EGG_MY_ERROR'
```
### `message` and `code`
An error should contains two `message` and `code` properties by default.
`message` maybe change cause by typo, but it won't cause `major` change, it juest `patch` or `minor` change.
`code` should keep stable after it first come out.
Contributor guide
Assessment
This issue has not been assessed yet.