Skip to content

Commit a56d710

Browse files
authored
Update CONTRIBUTING_JS.md
add section on error codes Ref. ipfs/js-ipfs#2547 (comment)
1 parent 051f97c commit a56d710

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

CONTRIBUTING_JS.md

+20
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Our toolkit for each of these is not set in stone, and we don't plan to halt our
1919
- [Guidelines](#guidelines)
2020
- [Supported versions](#supported-versions)
2121
- [Linting & Code Style](#linting--code-style)
22+
- [Error Codes](#error-codes)
2223
- [Dependency Versions](#dependency-versions)
2324
- [Testing](#testing)
2425
- [Releasing](#releasing)
@@ -100,6 +101,25 @@ However, we've added an extra linting rule: Enforce the use of [strict mode](htt
100101

101102
Using [aegir-lint](#aegir) will help you do this easily; it automatically lints your code.
102103

104+
#### Error Codes
105+
106+
When introducing a new error code that may be useful outside of the current scope, make sure it is defined in a named variable and can be exported and used in other places. This enables others to reuse those definitions and decreases number of hardcoded values across our codebases. For example:
107+
108+
```js
109+
const { Errors } = require('interface-datastore')
110+
111+
// throw predefined errors if possible
112+
if (!value) {
113+
throw Errors.notFoundError()
114+
}
115+
116+
// compare value from
117+
const ERR_NOT_FOUND = Errors.notFoundError().code
118+
if (err.code === ERR_NOT_FOUND) {
119+
// handle
120+
}
121+
```
122+
103123
#### Dependency Versions
104124

105125
Our rule is: Use ~ for everything below 1.0.0 and ^ for everything above 1.0.0. If you find a package.json that is not following this rule, please submit a PR.

0 commit comments

Comments
 (0)