Skip to content
This repository has been archived by the owner on Aug 31, 2024. It is now read-only.

Translate to chinese #68

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Translate error_handling into Chinese
  • Loading branch information
AruSeito committed Sep 3, 2020
commit 28ee2d7c6539d09db551af08a269a08c6db321ed
38 changes: 38 additions & 0 deletions exercises/error_handling/problem.zh-cn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
为下面的koa程序添加一个错误处理程序中间件。
`errorHandler` 需要可以捕获下游所有错误,
响应客户端`internal server error` 并且状态码为 `500`.

```
const Koa = require('koa');

const app = new Koa();

app.use(errorHandler());

app.use(async ctx => {
if (ctx.path === '/error') throw new Error('ooops');
ctx.body = 'OK';
});

function errorHandler() {
return async (ctx, next) => {
// 尝试捕获下游所有错误
};
}

app.listen(process.argv[2]);

```


提示

在Koa中,错误处理是通过`try/catch`完成的(除了event emitters) .如果您使用的是Express和其他Node框架,那么您可能已经很久没有看到这种情况了。与Express不同,错误处理只是添加到中间件堆栈顶部的装饰器。

您可以通过下述代码设定状态码:

```
ctx.status = 404;
```

每个Koa程序都是一个EventEmitter实例。 任何中间件未捕获到的所有错误都将发送到 `app.on('error', function (err, context) {})`. 这对于日志记录非常有用。但是,如果您创建自己的错误处理程序(即捕捉它),则必须自己手动发出这些事件。
28 changes: 28 additions & 0 deletions exercises/error_handling/solution_zh-cn/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const Koa = require('koa');

const app = new Koa();

app.use(errorHandler());

app.use(async ctx => {
if (ctx.path === '/error') throw new Error('ooops');
ctx.body = 'OK';
});

function errorHandler() {
return async (ctx, next) => {
// we catch all downstream errors here
try {
await next();
} catch (err) {
// set response status
ctx.status = 500;
// set response body
ctx.body = 'internal server error';
// can emit on app for log
// app.emit('error', err, ctx);
}
};
}

app.listen(process.argv[2]);
4 changes: 4 additions & 0 deletions exercises/error_handling/solution_zh-cn/solution.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
responds "OK" when requesting `/`
response status is 200 when requesting `/`
responds "internal server error" when requesting `/error`
response status is 500 when requesting `/error`