| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 48d1ea5 commit 8fd7184
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1188,10 +1188,15 @@ assert.throws( | |||
| 1188 | 1188 | assert.throws( | |
| 1189 | 1189 | () => { | |
| 1190 | 1190 | const otherErr = new Error('Not found'); | |
| 1191 | - otherErr.code = 404; | ||
| 1191 | + // Copy all enumerable properties from `err` to `otherErr`. | ||
| 1192 | + for (const [key, value] of Object.entries(err)) { | ||
| 1193 | + otherErr[key] = value; | ||
| 1194 | + } | ||
| 1192 | 1195 | throw otherErr; | |
| 1193 | 1196 | }, | |
| 1194 | - err // This tests for `message`, `name` and `code`. | ||
| 1197 | + // The error's `message` and `name` properties will also be checked when using | ||
| 1198 | + // an error as validation object. | ||
| 1199 | + err | ||
| 1195 | 1200 | ); | |
| 1196 | 1201 | ``` | |
| 1197 | 1202 | ||
@@ -1234,9 +1239,10 @@ assert.throws( | |||
| 1234 | 1239 | assert(err instanceof Error); | |
| 1235 | 1240 | assert(/value/.test(err)); | |
| 1236 | 1241 | // Returning anything from validation functions besides `true` is not | |
| 1237 | - // recommended. Doing so results in the caught error being thrown again. | ||
| 1238 | - // That is usually not the desired outcome. Throw an error about the | ||
| 1239 | - // specific validation that failed instead (as done in this example). | ||
| 1242 | + // recommended. By doing that, it's not clear what part of the validation | ||
| 1243 | + // failed. Instead, throw an error about the specific validation that failed | ||
| 1244 | + // (as done in this example) and add as much helpful debugging information | ||
| 1245 | + // to that error as possible. | ||
| 1240 | 1246 | return true; | |
| 1241 | 1247 | }, | |
| 1242 | 1248 | 'unexpected error' | |
@@ -1278,11 +1284,9 @@ assert.throws(notThrowing, 'Second'); | |||
| 1278 | 1284 | // It does not throw because the error messages match. | |
| 1279 | 1285 | assert.throws(throwingSecond, /Second$/); | |
| 1280 | 1286 | ||
| 1281 | - // If the error message does not match, the error from within the function is | ||
| 1282 | - // not caught. | ||
| 1287 | + // If the error message does not match, an AssertionError is thrown. | ||
| 1283 | 1288 | assert.throws(throwingFirst, /Second$/); | |
| 1284 | - // Error: First | ||
| 1285 | - // at throwingFirst (repl:2:9) | ||
| 1289 | + // AssertionError [ERR_ASSERTION] | ||
| 1286 | 1290 | ``` | |
| 1287 | 1291 | ||
| 1288 | 1292 | Due to the confusing notation, it is recommended not to use a string as the | |
| Back | FazBrowse Home | New Git URL |
0 commit comments