-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (34 loc) · 1.11 KB
/
index.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
const Charlotte = require('./src/charlotte')
module.exports = (options) => {
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
const charlotte = new Charlotte()
charlotte.prepareMailing(options.mail, options.onMail)
return async function charlotteMiddleware (ctx, next) {
charlotte.startLog()
charlotte.context(ctx)
try {
const route = charlotte.matchRoute(ctx.path)
if (route) {
await route.handler.call(charlotte)
}
else {
await next()
}
charlotte.endLog()
charlotte.log()
}
catch (err) {
ctx.status = 500
charlotte.addTraceback(err)
if (process.env.NODE_ENV === 'development') {
ctx.body = await charlotte.renderException(err)
}
charlotte.endLog()
charlotte.log()
// in production rethrow for custom error handling
if (process.env.NODE_ENV !== 'development') {
throw err
}
}
}
}