eggjs / eggjs/egg

Common egg Error Codes

Open
#1,047 18 comments 4 reactions 1 assignee Claimed by @popomore View on GitHub
Inactive type: discussion type: document type: feature
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.