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: 1-js/10-error-handling/2-custom-errors/article.md
+26-26Lines changed: 26 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,35 +8,35 @@
8
8
9
9
همانطور که برنامه رشد میکند، طبیعتا ارورهای ما یک سلسه مراتب تشکیل میدهند. برای مثال، `HttpTimeoutError` ممکن است از `HttpError` ارثبری کند و همینطور ادامه داشته باشد.
10
10
11
-
## Extending Error
11
+
## تعمیم دادن Error
12
12
13
-
As an example, let's consider a function `readUser(json)`that should read JSON with user data.
13
+
به عنوان یک مثال، بیایید تابع `readUser(json)`را در نظر بگیریم که جیسان حاوی داده کاربر را میخواند.
14
14
15
-
Here's an example of how a valid `json`may look:
15
+
اینجا مثالی از اینکه یک `json`معتبر چگونه است داریم:
16
16
```js
17
17
let json =`{ "name": "John", "age": 30 }`;
18
18
```
19
19
20
-
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.
20
+
از درون، ما از `JSON.parse` استفاده خواهیم کرد. اگر این متد یک`json` ناقص را دریافت کند، سپس `SyntaxError` پرتاب میکند. اما اگر `json`از لحاظ سینتکس درست باشد به معنی یک کاربر معتبر نیست نه؟ ممکن است که داده مهم را نداشته باشد. برای مثال، ممکن است ویژگیهای `name`و`age`که برای کاربران ما ضروری است را نداشته باشد.
21
21
22
-
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.
22
+
تابع `readUser(json)`نه تنها جیسان را میخواند بلکه داده را بررسی («اعتبارسنجی») میکند. اگر فیلدهای مورد نیاز وجود نداشته باشند یا شکل اشتباه باشد، پس یک ارور داریم. و این یک `SyntaxError` نیست چون داده از لحاظ سینتکس درست است بلکه نوع دیگری از ارور است. ما به آن`ValidationError`(ارور اعتبارسنجی) میگوییم و برای آن یک کلاس میسازیم. یک ارور از این نوع باید اطلاعاتی درباره فیلد متخلف را داشته باشد.
23
23
24
-
Our`ValidationError`class should inherit from the `Error`class.
24
+
کلاس`ValidationError`ما باید از کلاس `Error`ارثبری کند.
25
25
26
-
The`Error`class is built-in, but here's its approximate code so we can understand what we're extending:
26
+
کلاس`Error`درونساخت است اما اینجا کد تقریبی آن را داریم تا بتوانیم متوجه شویم که چه چیزی را تعمیم میدهیم:
27
27
28
28
```js
29
-
//The "pseudocode" for the built-in Error class defined by JavaScript itself
29
+
//درونساخت که توسط خود جاوااسکریپت تعریف شده است Error یک «شبه کد» برای کلاس
30
30
classError {
31
31
constructor(message) {
32
32
this.message= message;
33
-
this.name="Error"; // (different names for different built-in error classes)
34
-
this.stack=<call stack>; //non-standard, but most environments support it
33
+
this.name="Error"; // (اسمهای متفاوت برای کلاسهای ارور درونساخت متفاوت)
34
+
this.stack=<call stack>; //غیر استاندارد، اما اکثر محیطهای اجرا از آن پشتیبانی میکنند
35
35
}
36
36
}
37
37
```
38
38
39
-
Now let's inherit`ValidationError`from it and try it in action:
39
+
حالا بیایید با`ValidationError`آن را ارثبری کنیم و در عمل امتحانش کنیم:
40
40
41
41
```js run untrusted
42
42
*!*
@@ -57,15 +57,15 @@ try {
57
57
} catch(err) {
58
58
alert(err.message); // Whoops!
59
59
alert(err.name); // ValidationError
60
-
alert(err.stack); //a list of nested calls with line numbers for each
60
+
alert(err.stack); //لیستی از فراخوانیهای تودرتو با شماره خطوط برای هر کدام از آنها
61
61
}
62
62
```
63
63
64
-
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.
64
+
لطفا توجه کنید: در خط `(1)`ما تابع سازنده والد را فراخوانی میکنیم. جاوااسکریپت از ما میخواهد که`super`را درون تابع سازنده فرزند فراخوانی کنیم پس این موضوع الزامی است. تابع سازنده والد ویژگی `message`را تنظیم میکند.
65
65
66
-
The parent constructor also sets the `name`property to `"Error"`, so in the line `(2)`we reset it to the right value.
66
+
تابع سازنده والد همچنین ویژگی `name`را برابر با `"Error"` قرار میدهد پس در خط `(2)`ما آن را به مقدار درستش برمیگردانیم.
67
67
68
-
Let's try to use it in `readUser(json)`:
68
+
بیایید در `readUser(json)` از آن استفاده کنیم:
69
69
70
70
```js run
71
71
classValidationErrorextendsError {
@@ -75,7 +75,7 @@ class ValidationError extends Error {
75
75
}
76
76
}
77
77
78
-
//Usage
78
+
//کاربرد
79
79
functionreadUser(json) {
80
80
let user =JSON.parse(json);
81
81
@@ -89,39 +89,39 @@ function readUser(json) {
89
89
return user;
90
90
}
91
91
92
-
//Working example with try..catch
92
+
// try..catch مثال عملی با
93
93
94
94
try {
95
95
let user =readUser('{ "age": 25 }');
96
96
} catch (err) {
97
97
if (err instanceof ValidationError) {
98
98
*!*
99
-
alert("Invalid data: "+err.message); // Invalid data: No field: name
99
+
alert("داده نامعتبر: "+err.message); // Invalid data: No field: name
100
100
*/!*
101
101
} elseif (err instanceofSyntaxError) { // (*)
102
-
alert("JSON Syntax Error: "+err.message);
102
+
alert("ارور سینتکس جیسان: "+err.message);
103
103
} else {
104
-
throw err; //unknown error, rethrow it (**)
104
+
throw err; //کن rethrow ارور ناشناس، آن را (**)
105
105
}
106
106
}
107
107
```
108
108
109
-
The`try..catch`block in the code above handles both our `ValidationError`and the built-in`SyntaxError`from `JSON.parse`.
109
+
بلوک`try..catch`در کد بالا هم `ValidationError`ما و هم`SyntaxError`درونساخت را از `JSON.parse` مدیریت میکند.
110
110
111
-
Please take a look at how we use `instanceof`to check for the specific error type in the line `(*)`.
111
+
لطفا به اینکه ما چگونه از `instanceof`برای چک کردن یک نوع ارور خاص در خط `(*)` استفاده کردیم توجه کنید.
112
112
113
-
We could also look at `err.name`, like this:
113
+
همچنین میتوانستیم `err.name` را بررسی کنیم، مثلا اینگونه:
114
114
115
115
```js
116
116
// ...
117
-
//instead of (err instanceof SyntaxError)
117
+
// (err instanceof SyntaxError) به جای
118
118
} elseif (err.name=="SyntaxError") { // (*)
119
119
// ...
120
120
```
121
121
122
-
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.
122
+
نسخه`instanceof`خیلی بهتر است چون در آینده ما قرار است `ValidationError` را تعمیم دهیم، از آن انواع دیگر بسازیم، مثلا`PropertyRequiredError`. و بررسی `instanceof`برای کلاسهای ارثبر جدید هم کار خواهد کرد. پس این روش بعید است که منسوخ شود.
123
123
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.
124
+
همچنین مهم است که اگر`catch`یک ارور ناشناس را ملاقات کند، در خط `(**)` آن را rethrow کند. بلوک`catch`فقط میداند که چگونه ارورهای سینتکس و اعتبارسنجی را مدیریت کند، انواع دیگر (که به خاطر یک غلط املایی در کد یا هر دلیل دیگری ایجاد شدهاند) باید از آن بیرون بیافتند.
0 commit comments