You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CONTRIBUTING_JS.md
+36
Original file line number
Diff line number
Diff 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
19
19
-[Guidelines](#guidelines)
20
20
-[Supported versions](#supported-versions)
21
21
-[Linting & Code Style](#linting--code-style)
22
+
-[Error Codes](#error-codes)
22
23
-[Dependency Versions](#dependency-versions)
23
24
-[Testing](#testing)
24
25
-[Releasing](#releasing)
@@ -100,6 +101,41 @@ However, we've added an extra linting rule: Enforce the use of [strict mode](htt
100
101
101
102
Using [aegir-lint](#aegir) will help you do this easily; it automatically lints your code.
102
103
104
+
#### Error Codes
105
+
106
+
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:
107
+
108
+
```js
109
+
classNotFoundErrorextendsError {
110
+
constructor (message) {
111
+
super(message ||'Resource was not found')
112
+
this.name='NotFoundError'
113
+
this.code=NotFoundError.code
114
+
}
115
+
}
116
+
117
+
NotFoundError.code='ERR_NOT_FOUND'
118
+
exports.NotFoundError= NotFoundError
119
+
```
120
+
121
+
122
+
This enables others to reuse those definitions and decreases the number of hardcoded values across our codebases.
123
+
For example:
124
+
125
+
```js
126
+
const { NotFoundError } =require('some-module')
127
+
128
+
// throw predefined errors types
129
+
if (!value) {
130
+
thrownewNotFoundError()
131
+
}
132
+
133
+
// compare against code from imported type
134
+
if (err.code===NotFoundError.code) {
135
+
// handle
136
+
}
137
+
```
138
+
103
139
#### Dependency Versions
104
140
105
141
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