Skip to content

Commit ec8b369

Browse files
committed
error handling
1 parent 472bbcc commit ec8b369

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

README.md

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,18 +2598,26 @@ try {
25982598

25992599
## Error Handling
26002600

2601+
## 错误处理
2602+
26012603
Thrown errors are a good thing! They mean the runtime has successfully identified when something in your program has gone wrong and it's letting you know by stopping function
26022604
execution on the current stack, killing the process (in Node), and notifying you in the console with a stack trace.
26032605

2606+
抛出错误是件好事! 他们意味着当程序出错时, 成功的通知运行时, 并通过停止执行当前堆栈上的函数, 终止进程(在 Node 中), 并且在控制台打印错误堆栈信息以通知你。
2607+
26042608
### Always use Error for throwing or rejecting
26052609

2610+
### 始终使用为抛出或拒绝使用错误对象 (Error)
2611+
26062612
JavaScript as well as TypeScript allow you to `throw` any object. A Promise can also be rejected with any reason object.
26072613
It is advisable to use the `throw` syntax with an `Error` type. This is because your error might be caught in higher level code with a `catch` syntax.
26082614
It would be very confusing to catch a string message there and would make
26092615
[debugging more painful](https://basarat.gitbooks.io/typescript/docs/types/exceptions.html#always-use-error).
26102616
For the same reason you should reject promises with `Error` types.
26112617

2612-
**Bad:**
2618+
JavaScript 以及 TypeScript 允许你 `抛出` 任意对象, 一个 Promise 也能够用任意对象进行拒绝。 使用 `抛出 (throw)` 语法和 `错误 (Error)` 类型是非常明智的, 这是因为错误消息可能会被更高级的语句用 `cache` 捕获到。 而捕获一个字符串可能会非常令人疑惑的, 同时也会让[调试更加痛苦](https://basarat.gitbooks.io/typescript/docs/types/exceptions.html#always-use-error)。 同样的理由, 拒绝 Promise 时, 也应该使用 `Error` 类型。
2619+
2620+
**不好的:**
26132621

26142622
```ts
26152623
function calculateTotal(items: Item[]): number {
@@ -2621,7 +2629,7 @@ function get(): Promise<Item[]> {
26212629
}
26222630
```
26232631

2624-
**Good:**
2632+
**好的:**
26252633

26262634
```ts
26272635
function calculateTotal(items: Item[]): number {
@@ -2644,6 +2652,8 @@ is very powerful for debugging.
26442652
There are also another alternatives, not to use the `throw` syntax and instead always return custom error objects. TypeScript makes this even easier.
26452653
Consider following example:
26462654

2655+
使用 `Error` 类型的好处是它被 `try/catch/finally` 支持, 并且所有的 Error 对象都有一个隐式属性 `stack` , 在调试时很有用。 还有一个选择, 那就是不使用 `throw` 语法, 始终返回自定义的错误对象。 TypeScript 下更加容易, 参看下面的例子:
2656+
26472657
```ts
26482658
type Result<R> = { isError: false, value: R };
26492659
type Failure<E> = { isError: true, error: E };
@@ -2661,13 +2671,19 @@ function calculateTotal(items: Item[]): Failable<number, 'empty'> {
26612671

26622672
For the detailed explanation of this idea refer to the [original post](https://medium.com/@dhruvrajvanshi/making-exceptions-type-safe-in-typescript-c4d200ee78e9).
26632673

2664-
**[⬆ back to top](#table-of-contents)**
2674+
要查看这个主意的更详细说明, 请参考[原帖](https://medium.com/@dhruvrajvanshi/making-exceptions-type-safe-in-typescript-c4d200ee78e9)
2675+
2676+
**[⬆ 返回目录](#目录)**
26652677

26662678
### Don't ignore caught errors
26672679

2680+
### 不要忽略捕获到的错误
2681+
26682682
Doing nothing with a caught error doesn't give you the ability to ever fix or react to said error. Logging the error to the console (`console.log`) isn't much better as often times it can get lost in a sea of things printed to the console. If you wrap any bit of code in a `try/catch` it means you think an error may occur there and therefore you should have a plan, or create a code path, for when it occurs.
26692683

2670-
**Bad:**
2684+
捕获到错误后,什么都不做, 既不能让你修复错误, 也不能让你响应错误。 使用 `console.log` 将错误输出到控制台并不是十分高明, 因为经常会有大量的内容被打印输出到控制台, 很难再被找到。 一旦你在 `try/catch` 中包括了任何一点儿代码, 这就意味着你认为这里可能会有错误发生, 你应当针对它有一个计划, 或者一段代码来进行处理。
2685+
2686+
**不好的:**
26712687

26722688
```ts
26732689
try {
@@ -2677,15 +2693,17 @@ try {
26772693
}
26782694

26792695
// or even worse
2696+
// 更糟糕的是
26802697

26812698
try {
26822699
functionThatMightThrow();
26832700
} catch (error) {
26842701
// ignore error
2702+
// 完全忽略错误
26852703
}
26862704
```
26872705

2688-
**Good:**
2706+
**好的:**
26892707

26902708
```ts
26912709
import { logger } from './logging'
@@ -2697,13 +2715,17 @@ try {
26972715
}
26982716
```
26992717

2700-
**[back to top](#table-of-contents)**
2718+
**[返回目录](#目录)**
27012719

27022720
### Don't ignore rejected promises
27032721

2722+
### 不要忽略被拒绝的 Promise
2723+
27042724
For the same reason you shouldn't ignore caught errors from `try/catch`.
27052725

2706-
**Bad:**
2726+
由于同样的原因, 你不应该忽略由 `try/catch` 捕获到的错误。
2727+
2728+
**不好的:**
27072729

27082730
```ts
27092731
getUser()
@@ -2715,7 +2737,7 @@ getUser()
27152737
});
27162738
```
27172739

2718-
**Good:**
2740+
**好的:**
27192741

27202742
```ts
27212743
import { logger } from './logging'
@@ -2729,6 +2751,7 @@ getUser()
27292751
});
27302752

27312753
// or using the async/await syntax:
2754+
// 或者使用 async/await 语法:
27322755

27332756
try {
27342757
const user = await getUser();
@@ -2738,7 +2761,7 @@ try {
27382761
}
27392762
```
27402763

2741-
**[back to top](#table-of-contents)**
2764+
**[返回目录](#目录)**
27422765

27432766
## Formatting
27442767

0 commit comments

Comments
 (0)