Skip to content

Add section on "Error Codes" to CONTRIBUTING_JS.md #445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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