错误
统一处理错误
注意
一定要放到第一个中间件。这样才能捕捉到错误。
typescript
import Koa from 'koa'
import Router from 'koa-router'
import bodyParser from 'koa-bodyparser'
import { log } from 'console'
const app = new Koa()
const router = new Router()
app.use(async (ctx: Koa.Context, next: Koa.Next) => {
try {
await next()
if (ctx.response.status === 404) {
ctx.body = {
'code': 4004,
'message': '在这里捕捉 404 然后设置返回内容'
}
}
} catch (e) {
ctx.status = 500
ctx.body = {
'code': -1,
'message': '统一捕捉的错误,也就是,所有的过程如果有错误,直接在这里就捕捉到了'
}
}
})
app
.use(bodyParser())
.use(router.routes())
router.get('/', async (ctx: Koa.Context) => {
ctx.status = 200
ctx.body = {
'1': '1'
}
})
router.get('/a', async (ctx: Koa.Context) => {
ctx.throw(500, 'error')
})
app.listen(3000, '0.0.0.0', () => {
console.log('服务器启动成功')
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47