minor
This commit is contained in:
parent
84ff065501
commit
78fbf9548d
1 changed files with 9 additions and 14 deletions
|
@ -17,17 +17,13 @@ Here's an example of how a valid `json` may look:
|
|||
let json = `{ "name": "John", "age": 30 }`;
|
||||
```
|
||||
|
||||
Internally, we'll use `JSON.parse`. If it receives malformed `json`, then it throws `SyntaxError`.
|
||||
|
||||
But even if `json` is syntactically correct, that doesn't mean that it's a valid user, right? It may miss the necessary data. For instance, it may not have `name` and `age` properties that are essential for our users.
|
||||
Internally, we'll use `JSON.parse`. If it receives malformed `json`, then it throws `SyntaxError`. But even if `json` is syntactically correct, that doesn't mean that it's a valid user, right? It may miss the necessary data. For instance, it may not have `name` and `age` properties that are essential for our users.
|
||||
|
||||
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.
|
||||
|
||||
Our `ValidationError` class should inherit from the built-in `Error` class.
|
||||
|
||||
That class is built-in, but we should have its approximate code before our eyes, to understand what we're extending.
|
||||
|
||||
So here you are:
|
||||
That class is built-in, here's it approximate code, for us to understand what we're extending:
|
||||
|
||||
```js
|
||||
// The "pseudocode" for the built-in Error class defined by JavaScript itself
|
||||
|
@ -35,12 +31,12 @@ class Error {
|
|||
constructor(message) {
|
||||
this.message = message;
|
||||
this.name = "Error"; // (different names for different built-in error classes)
|
||||
this.stack = <nested calls>; // non-standard, but most environments support it
|
||||
this.stack = <call stack>; // non-standard, but most environments support it
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Now let's go on and inherit `ValidationError` from it:
|
||||
Now let's inherit `ValidationError` from it and try it in action:
|
||||
|
||||
```js run untrusted
|
||||
*!*
|
||||
|
@ -65,10 +61,9 @@ try {
|
|||
}
|
||||
```
|
||||
|
||||
Please take a look at the constructor:
|
||||
Please note: in the line `(1)` we call the parent constructor. JavaScript requires us to call `super` in the child constructor, so that's obligatory. The parent constructor sets the `message` property.
|
||||
|
||||
1. In the line `(1)` we call the parent constructor. JavaScript requires us to call `super` in the child constructor, so that's obligatory. The parent constructor sets the `message` property.
|
||||
2. The parent constructor also sets the `name` property to `"Error"`, so in the line `(2)` we reset it to the right value.
|
||||
The parent constructor also sets the `name` property to `"Error"`, so in the line `(2)` we reset it to the right value.
|
||||
|
||||
Let's try to use it in `readUser(json)`:
|
||||
|
||||
|
@ -185,7 +180,7 @@ try {
|
|||
|
||||
The new class `PropertyRequiredError` is easy to use: we only need to pass the property name: `new PropertyRequiredError(property)`. The human-readable `message` is generated by the constructor.
|
||||
|
||||
Please note that `this.name` in `PropertyRequiredError` constructor is again assigned manually. That may become a bit tedious -- to assign `this.name = <class name>` in every custom error class. But there's a way out. We can make our own "basic error" class that removes this burden from our shoulders by using `this.constructor.name` for `this.name` in its constructor. And then inherit all ours custom errors from it.
|
||||
Please note that `this.name` in `PropertyRequiredError` constructor is again assigned manually. That may become a bit tedious -- to assign `this.name = <class name>` in every custom error class. We can avoid it by making our own "basic error" class that assigns `this.name = this.constructor.name`. And then inherit all ours custom errors from it.
|
||||
|
||||
Let's call it `MyError`.
|
||||
|
||||
|
@ -218,9 +213,9 @@ Now custom errors are much shorter, especially `ValidationError`, as we got rid
|
|||
|
||||
## Wrapping exceptions
|
||||
|
||||
The purpose of the function `readUser` in the code above is "to read the user data", right? There may occur different kinds of errors in the process. Right now we have `SyntaxError` and `ValidationError`, but in the future `readUser` function may grow and probably generate other kinds of errors.
|
||||
The purpose of the function `readUser` in the code above is "to read the user data". There may occur different kinds of errors in the process. Right now we have `SyntaxError` and `ValidationError`, but in the future `readUser` function may grow and probably generate other kinds of errors.
|
||||
|
||||
The code which calls `readUser` should handle these errors. Right now it uses multiple `if` in the `catch` block to check for different error types and rethrow the unknown ones. But if `readUser` function generates several kinds of errors -- then we should ask ourselves: do we really want to check for all error types one-by-one in every code that calls `readUser`?
|
||||
The code which calls `readUser` should handle these errors. Right now it uses multiple `if` in the `catch` block, that check the class and handle known errors and rethrow the unknown ones. But if `readUser` function generates several kinds of errors -- then we should ask ourselves: do we really want to check for all error types one-by-one in every code that calls `readUser`?
|
||||
|
||||
Often the answer is "No": the outer code wants to be "one level above all that". It wants to have some kind of "data reading error". Why exactly it happened -- is often irrelevant (the error message describes it). Or, even better if there is a way to get error details, but only if we need to.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue