Skip to content

Commit

Permalink
Add section on "Error Codes" to CONTRIBUTING_JS.md (#445)
Browse files Browse the repository at this point in the history
* Update CONTRIBUTING_JS.md

add section on error codes
Ref. ipfs/js-ipfs#2547 (comment)

* Update CONTRIBUTING_JS.md

Co-Authored-By: Teri Chadbourne <[email protected]>

* docs: add Error Codes example

License: MIT
Signed-off-by: Marcin Rataj <[email protected]>

* Update CONTRIBUTING_JS.md

Co-Authored-By: Teri Chadbourne <[email protected]>
  • Loading branch information
2 people authored and Alan Shaw committed Nov 8, 2019
1 parent 051f97c commit a14dc0a
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions CONTRIBUTING_JS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Our toolkit for each of these is not set in stone, and we don't plan to halt our
- [Guidelines](#guidelines)
- [Supported versions](#supported-versions)
- [Linting & Code Style](#linting--code-style)
- [Error Codes](#error-codes)
- [Dependency Versions](#dependency-versions)
- [Testing](#testing)
- [Releasing](#releasing)
Expand Down Expand Up @@ -100,6 +101,41 @@ However, we've added an extra linting rule: Enforce the use of [strict mode](htt

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

#### Error Codes

When introducing a new error code that may be useful outside of the current scope, make sure it is exported as a new `Error` type:

```js
class NotFoundError extends Error {
constructor (message) {
super(message || 'Resource was not found')
this.name = 'NotFoundError'
this.code = NotFoundError.code
}
}

NotFoundError.code = 'ERR_NOT_FOUND'
exports.NotFoundError = NotFoundError
```


This enables others to reuse those definitions and decreases the number of hardcoded values across our codebases.
For example:

```js
const { NotFoundError } = require('some-module')

// throw predefined errors types
if (!value) {
throw new NotFoundError()
}

// compare against code from imported type
if (err.code === NotFoundError.code) {
// handle
}
```

#### Dependency Versions

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.
Expand Down

0 comments on commit a14dc0a

Please sign in to comment.