-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy patherrors.js
48 lines (37 loc) · 985 Bytes
/
errors.js
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
48
/**
* This example illustrates how application
* errors propagate upstream for
* centralized logging etc.
*/
const mount = require('..')
const Koa = require('koa')
const a = new Koa()
const b = new Koa()
const c = new Koa()
a.use(async function (ctx, next) {
await next()
})
b.use(async function (ctx, next) {
await next()
})
c.use(async function (ctx, next) {
await next()
throw new Error('tobi escaped!')
})
a.use(mount(b))
b.use(mount(c))
// suppress stderr output if you want
a.outputErrors = false
// errors will propagate to the upstream app,
// however you can still use the "error" listener
// in subapps more specific behaviour, just keep
// in mind that it should be used in an unobtrusive way,
// since most decisions should be made by the parent app.
a.on('error', function (err, ctx) {
console.log('%s %s: sending error "%s" to alert service',
ctx.method,
ctx.path,
err.message)
})
a.listen(3000)
console.log('listening on port 3000')