Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7725acc

Browse files
committedJun 26, 2021
minor fixes
1 parent 9bba570 commit 7725acc

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed
 

‎1-js/10-error-handling/2-custom-errors/article.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ Internally, we'll use `JSON.parse`. If it receives malformed `json`, then it thr
2121

2222
Our function `readUser(json)` will not only read JSON, but check ("validate") the data. If there are no required fields, or the format is wrong, then that's an error. And that's not a `SyntaxError`, because the data is syntactically correct, but another kind of error. We'll call it `ValidationError` and create a class for it. An error of that kind should also carry the information about the offending field.
2323

24-
Our `ValidationError` class should inherit from the built-in `Error` class.
24+
Our `ValidationError` class should inherit from the `Error` class.
2525

26-
That class is built-in, but here's its approximate code so we can understand what we're extending:
26+
The `Error` class is built-in, but here's its approximate code so we can understand what we're extending:
2727

2828
```js
2929
// The "pseudocode" for the built-in Error class defined by JavaScript itself
@@ -117,15 +117,15 @@ We could also look at `err.name`, like this:
117117
// instead of (err instanceof SyntaxError)
118118
} else if (err.name == "SyntaxError") { // (*)
119119
// ...
120-
```
120+
```
121121
122122
The `instanceof` version is much better, because in the future we are going to extend `ValidationError`, make subtypes of it, like `PropertyRequiredError`. And `instanceof` check will continue to work for new inheriting classes. So that's future-proof.
123123
124-
Also it's important that if `catch` meets an unknown error, then it rethrows it in the line `(**)`. The `catch` block only knows how to handle validation and syntax errors, other kinds (due to a typo in the code or other unknown ones) should fall through.
124+
Also it's important that if `catch` meets an unknown error, then it rethrows it in the line `(**)`. The `catch` block only knows how to handle validation and syntax errors, other kinds (caused by a typo in the code or other unknown reasons) should fall through.
125125
126126
## Further inheritance
127127
128-
The `ValidationError` class is very generic. Many things may go wrong. The property may be absent or it may be in a wrong format (like a string value for `age`). Let's make a more concrete class `PropertyRequiredError`, exactly for absent properties. It will carry additional information about the property that's missing.
128+
The `ValidationError` class is very generic. Many things may go wrong. The property may be absent or it may be in a wrong format (like a string value for `age` instead of a number). Let's make a more concrete class `PropertyRequiredError`, exactly for absent properties. It will carry additional information about the property that's missing.
129129
130130
```js run
131131
class ValidationError extends Error {

0 commit comments

Comments
 (0)
Please sign in to comment.