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: docs/types/exceptions.md
+20-2Lines changed: 20 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,8 +57,24 @@ catch(e) {
57
57
console.log(e);
58
58
}
59
59
```
60
-
*Don't do that*. Such raw strings result in a very painful debugging and error handling experience.
60
+
*Don't do that*. The fundamental benefit of `Error` objects is that they automatically keep track of where they were built and originated as the `stack` property.
61
61
62
+
Raw strings result in a very painful debugging experience and complicate error analysis from logs.
63
+
64
+
## You don't have to `throw` an error
65
+
It is okay to pass an `Error` object around. This is conventional in NodeJS callback style code which take callbacks with the first argument as an error object.
66
+
67
+
```js
68
+
functionmyFunction (callback: (e?:Error)) {
69
+
doSomethingAsync(function () {
70
+
if (somethingWrong) {
71
+
callback(newError('This is my error'))
72
+
} else {
73
+
callback();
74
+
}
75
+
});
76
+
}
77
+
```
62
78
63
79
## Exceptional cases
64
80
`Exceptions should be exceptional` is a common saying in computer science. There are few resons why this is true for JavaScript (and TypeScript) as well.
@@ -127,4 +143,6 @@ function validate(value: number): {error?: string} {
127
143
if (value <0|| value >100) return {error:'Invalid value'};
128
144
}
129
145
```
130
-
And not its represented in the type system.
146
+
And now its represented in the type system.
147
+
148
+
> Unless you want to handle the error in a very generic (simple / catch-all etc) way, don't *throw* an error.
0 commit comments