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/11-async/04-promise-error-handling/article.md
+38-38Lines changed: 38 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
2
-
# Error handling with promises
2
+
# التعامل مع الأخطاء في الـ `promises`
3
3
4
-
Promise chains are great at error handling. When a promise rejects, the control jumps to the closest rejection handler. That's very convenient in practice.
4
+
سلاسل الـ `promises` رائعة في معالجة الأخطاء. عندما يرفض الوعد ، يقفز عنصر التحكم إلى أقرب معالج رفض. هذا مريح للغاية في الممارسة.
5
5
6
-
For instance, in the code below the URL to `fetch` is wrong (no such site) and `.catch` handles the error:
6
+
على سبيل المثال ، في التعليمات البرمجية أسفل عنوان URL لـ `جلب 'غير صحيح (لا يوجد مثل هذا الموقع) ويتعامل` .atch' مع الخطأ:
.catch(err=>alert(err)) // TypeError: failed to fetch (the text may vary)
14
14
```
15
15
16
-
As you can see, the `.catch` doesn't have to be immediate. It may appear after one or maybe several `.then`.
16
+
كما ترى ، لا يجب أن يكون "الصيد" فوريًا. قد تظهر بعد واحد أو ربما عدة "ثم".
17
17
18
-
Or, maybe, everything is all right with the site, but the response is not valid JSON. The easiest way to catch all errors is to append `.catch` to the end of chain:
18
+
أو ، ربما ، كل شيء على ما يرام مع الموقع ، ولكن الإجابة غير صالحة لـ JSON. أسهل طريقة للقبض على جميع الأخطاء هي إلحاق ".catch" بنهاية السلسلة:
Normally, such `.catch` doesn't trigger at all. But if any of the promises above rejects (a network problem or invalid json or whatever), then it would catch it.
41
+
عادةً ، لا يتم تشغيل ".catch" على الإطلاق. ولكن إذا رفض أي من الوعود أعلاه (مشكلة في الشبكة أو json غير صالح أو أيا كان) ، فسوف يلتقطها.
42
42
43
43
## Implicit try..catch
44
44
45
-
The code of a promise executor and promise handlers has an "invisible `try..catch`" around it. If an exception happens, it gets caught and treated as a rejection.
45
+
رمز تنفيذ الوعد ومعالجو الوعد يحتوي على "محاولة غير مرئية" غير مرئية حوله. إذا حدث استثناء ، يتم القبض عليه ومعاملته على أنه رفض.
46
46
47
-
For instance, this code:
47
+
على سبيل المثال ، هذا الكود:
48
48
49
49
```js run
50
50
newPromise((resolve, reject) => {
@@ -54,7 +54,7 @@ new Promise((resolve, reject) => {
54
54
}).catch(alert); // Error: Whoops!
55
55
```
56
56
57
-
...Works exactly the same as this:
57
+
... يعمل تمامًا مثل هذا:
58
58
59
59
```js run
60
60
newPromise((resolve, reject) => {
@@ -64,11 +64,11 @@ new Promise((resolve, reject) => {
64
64
}).catch(alert); // Error: Whoops!
65
65
```
66
66
67
-
The "invisible `try..catch`" around the executor automatically catches the error and turns it into rejected promise.
67
+
الـ `try..catch` غير المرئية حول المنفذ تقوم بالتقاط الحطأ بشكل أوتوماتيكي ويحوله إلى `rejected promise`.
68
68
69
-
This happens not only in the executor function, but in its handlers as well. If we `throw` inside a `.then` handler, that means a rejected promise, so the control jumps to the nearest error handler.
69
+
يحدث هذا ليس فقط في وظيفة المنفذ ، ولكن في معالجاتها أيضًا. إذا قمنا "بإلقاء" داخل معالج ".then" ، فهذا يعني وعدًا مرفوضًا ، لذلك ينتقل عنصر التحكم إلى أقرب معالج أخطاء.
70
70
71
-
Here's an example:
71
+
إليك مثال:
72
72
73
73
```js run
74
74
newPromise((resolve, reject) => {
@@ -80,7 +80,7 @@ new Promise((resolve, reject) => {
80
80
}).catch(alert); // Error: Whoops!
81
81
```
82
82
83
-
This happens for all errors, not just those caused by the`throw` statement. For example, a programming error:
83
+
يحدث هذا لجميع الأخطاء ، وليس فقط تلك التي تسببها عبارة`throw`. على سبيل المثال ، خطأ في البرمجة:
84
84
85
85
```js run
86
86
newPromise((resolve, reject) => {
@@ -92,17 +92,17 @@ new Promise((resolve, reject) => {
92
92
}).catch(alert); // ReferenceError: blabla is not defined
93
93
```
94
94
95
-
The final `.catch` not only catches explicit rejections, but also accidental errors in the handlers above.
95
+
لا يكتفي "catch" النهائي برفض صريح فحسب ، بل يشتمل أيضًا على أخطاء عرضية في المعالجات أعلاه.
96
96
97
-
## Rethrowing
97
+
## إعادة رمي الخطأ
98
98
99
-
As we already noticed, `.catch` at the end of the chain is similar to `try..catch`. We may have as many `.then` handlers as we want, and then use a single `.catch` at the end to handle errors in all of them.
99
+
كما لاحظنا بالفعل ، ".catch" في نهاية السلسلة يشبه "try..catch". قد يكون لدينا العدد الذي تريده من معالجات "then`" ، ثم نستخدم ".catch" واحدًا في النهاية للتعامل مع الأخطاء في جميعها.
100
100
101
-
In a regular `try..catch` we can analyze the error and maybe rethrow it if it can't be handled. The same thing is possible for promises.
101
+
في "المحاولة .. المصيد" العادية ، يمكننا تحليل الخطأ وربما إعادة ترميمه إذا لم يكن من الممكن معالجته. نفس الشيء ممكن للوعود.
102
102
103
-
If we `throw` inside `.catch`, then the control goes to the next closest error handler. And if we handle the error and finish normally, then it continues to the next closest successful `.then`handler.
103
+
إذا قمنا `بإلقاء` داخل`.catch` ، فسينتقل عنصر التحكم إلى أقرب معالج أخطاء التالي. وإذا تعاملنا مع الخطأ وانتهينا بشكل طبيعي ، فسيستمر إلى أقرب معالج `.then`التالي.
104
104
105
-
In the example below the `.catch`successfully handles the error:
105
+
في المثال أدناه يتعامل `catch`بنجاح مع الخطأ:
106
106
107
107
```js run
108
108
// the execution: catch -> then
@@ -117,9 +117,9 @@ new Promise((resolve, reject) => {
Here the `.catch` block finishes normally. So the next successful `.then` handler is called.
120
+
هنا تنتهي كتلة "الصيد" بشكل طبيعي. لذلك يتم استدعاء معالج ".then" الناجح التالي.
121
121
122
-
In the example below we see the other situation with `.catch`. The handler `(*)`catches the error and just can't handle it (e.g. it only knows how to handle `URIError`), so it throws it again:
122
+
في المثال أدناه نرى الموقف الآخر مع ".catch". معالج `(*)`يمسك الخطأ ولا يمكنه التعامل معه (على سبيل المثال ، فهو يعرف فقط كيفية التعامل مع `URIError`) ، لذا فإنه يرميه مرة أخرى:
123
123
124
124
```js run
125
125
// the execution: catch -> catch -> then
@@ -149,11 +149,11 @@ new Promise((resolve, reject) => {
149
149
});
150
150
```
151
151
152
-
The execution jumps from the first `.catch``(*)`to the next one `(**)`down the chain.
152
+
يقفز التنفيذ من أول ".catch"`(*)`إلى التالي `(**)`أسفل السلسلة.
153
153
154
-
## Unhandled rejections
154
+
## حالات الرفض التي لم تتم معالجتها
155
155
156
-
What happens when an error is not handled? For instance, we forgot to append `.catch` to the end of the chain, like here:
156
+
ماذا يحدث عندما لا يتم معالجة الخطأ؟ على سبيل المثال ، نسينا إلحاق "الصيد" بنهاية السلسلة ، كما يلي:
157
157
158
158
```js untrusted run refresh
159
159
newPromise(function() {
@@ -164,15 +164,15 @@ new Promise(function() {
164
164
}); // without .catch at the end!
165
165
```
166
166
167
-
In case of an error, the promise becomes rejected, and the execution should jump to the closest rejection handler. But there is none. So the error gets "stuck". There's no code to handle it.
167
+
Iفي حالة وجود خطأ ، يصبح الوعد مرفوضًا ، ويجب أن يقفز التنفيذ إلى أقرب معالج رفض. ولكن لا يوجد شيء. لذلك "عالق" الخطأ. لا يوجد رمز للتعامل معها.
168
168
169
-
In practice, just like with regular unhandled errors in code, it means that something has gone terribly wrong.
169
+
من الناحية العملية ، تمامًا كما هو الحال مع الأخطاء العادية التي لم تتم معالجتها في التعليمات البرمجية ، فهذا يعني أن شيئًا ما قد حدث خطأ فادحًا.
170
170
171
-
What happens when a regular error occurs and is not caught by `try..catch`? The script dies with a message in the console. A similar thing happens with unhandled promise rejections.
171
+
ماذا يحدث عند حدوث خطأ عادي ولا يتم اكتشافه بواسطة "try..catch"؟ يموت البرنامج النصي مع رسالة في وحدة التحكم. يحدث شيء مماثل مع رفض الوعد غير المعالج.
172
172
173
-
The JavaScript engine tracks such rejections and generates a global error in that case. You can see it in the console if you run the example above.
173
+
يتتبع محرك جافا سكريبت حالات الرفض هذه ويولد خطأً عامًا في هذه الحالة. يمكنك رؤيته في وحدة التحكم إذا قمت بتشغيل المثال أعلاه.
174
174
175
-
In the browser we can catch such errors using the event`unhandledrejection`:
175
+
في المتصفح يمكننا اكتشاف مثل هذه الأخطاء باستخدام الحدث`unhandledrejection`:
176
176
177
177
```js run
178
178
*!*
@@ -188,17 +188,17 @@ new Promise(function() {
188
188
}); // no catch to handle the error
189
189
```
190
190
191
-
The event is the part of the [HTML standard](https://html.spec.whatwg.org/multipage/webappapis.html#unhandled-promise-rejections).
191
+
الحدث هو جزء من [معيار HTML](https://html.spec.whatwg.org/multipage/webappapis.html#unhandled-promise-rejections).
192
192
193
-
If an error occurs, and there's no `.catch`, the `unhandledrejection` handler triggers, and gets the `event` object with the information about the error, so we can do something.
193
+
إذا حدث خطأ ، ولم يكن هناك "التقاط" ، فسيتم تشغيل معالج "unhandledrejection" ، ويحصل على كائن "الحدث" مع معلومات حول الخطأ ، حتى نتمكن من القيام بشيء ما.
194
194
195
-
Usually such errors are unrecoverable, so our best way out is to inform the user about the problem and probably report the incident to the server.
195
+
عادة ما تكون مثل هذه الأخطاء غير قابلة للاسترداد ، لذلك أفضل طريقة للخروج هي إبلاغ المستخدم بالمشكلة وربما الإبلاغ عن الحادث إلى الخادم.
196
196
197
-
In non-browser environments like Node.js there are other ways to track unhandled errors.
197
+
في البيئات غير المستعرضة مثل Node.js ، هناك طرق أخرى لتتبع الأخطاء التي لم تتم معالجتها.
198
198
199
-
## Summary
199
+
## ملخص
200
200
201
-
-`.catch`handles errors in promises of all kinds: be it a `reject()`call, or an error thrown in a handler.
202
-
-We should place `.catch` exactly in places where we want to handle errors and know how to handle them. The handler should analyze errors (custom error classes help) and rethrow unknown ones (maybe they are programming mistakes).
203
-
-It's ok not to use `.catch` at all, if there's no way to recover from an error.
204
-
-In any case we should have the `unhandledrejection`event handler (for browsers, and analogs for other environments) to track unhandled errors and inform the user (and probably our server) about them, so that our app never "just dies".
201
+
-يعالج `.catch`الأخطاء في الوعود بجميع أنواعها: سواء كان استدعاء``رفض ()` أو خطأ تم طرحه في معالج.
202
+
-يجب وضع ".catch" بالضبط في الأماكن التي نرغب في معالجة الأخطاء فيها ومعرفة كيفية التعامل معها. يجب أن يقوم المعالج بتحليل الأخطاء (تساعد فئات الأخطاء المخصصة) وإعادة إنشاء الأخطاء غير المعروفة (ربما تكون أخطاء برمجة).
203
+
-لا بأس بعدم استخدام "الصيد" على الإطلاق ، إذا لم تكن هناك طريقة للتعافي من خطأ.
204
+
-على أي حال ، يجب أن يكون لدينا معالج الأحداث `unhandledrejection`(للمتصفحات والنظير للبيئات الأخرى) لتتبع الأخطاء التي لم تتم معالجتها وإبلاغ المستخدم (وربما خادمنا) بشأنها ، حتى لا يموت تطبيقنا أبدًا.
0 commit comments