|
6 | 6 | */
|
7 | 7 |
|
8 | 8 | const debug = require('debug')('koa-static')
|
9 |
| -const resolve = require('path').resolve |
| 9 | +const { resolve } = require('path') |
10 | 10 | const assert = require('assert')
|
11 | 11 | const send = require('koa-send')
|
12 | 12 |
|
@@ -36,25 +36,31 @@ function serve (root, opts) {
|
36 | 36 | if (opts.index !== false) opts.index = opts.index || 'index.html'
|
37 | 37 |
|
38 | 38 | if (!opts.defer) {
|
39 |
| - return function serve (ctx, next) { |
| 39 | + return async function serve (ctx, next) { |
40 | 40 | if (ctx.method === 'HEAD' || ctx.method === 'GET') {
|
41 |
| - return send(ctx, ctx.path, opts).then(done => { |
42 |
| - if (!done) { |
43 |
| - return next() |
44 |
| - } |
45 |
| - }) |
| 41 | + try { |
| 42 | + await send(ctx, ctx.path, opts) |
| 43 | + } catch (err) { |
| 44 | + await next() |
| 45 | + } |
| 46 | + return |
46 | 47 | }
|
47 |
| - return next() |
| 48 | + |
| 49 | + await next() |
48 | 50 | }
|
49 | 51 | }
|
50 | 52 |
|
51 |
| - return function serve (ctx, next) { |
52 |
| - return next().then(() => { |
53 |
| - if (ctx.method !== 'HEAD' && ctx.method !== 'GET') return |
54 |
| - // response is already handled |
55 |
| - if (ctx.body != null || ctx.status !== 404) return // eslint-disable-line |
| 53 | + return async function serve (ctx, next) { |
| 54 | + await next() |
| 55 | + |
| 56 | + if (ctx.method !== 'HEAD' && ctx.method !== 'GET') return |
| 57 | + // response is already handled |
| 58 | + if (ctx.body != null || ctx.status !== 404) return // eslint-disable-line |
56 | 59 |
|
57 |
| - return send(ctx, ctx.path, opts) |
58 |
| - }) |
| 60 | + try { |
| 61 | + await send(ctx, ctx.path, opts) |
| 62 | + } catch (err) { |
| 63 | + ctx.status = err.status || 500 |
| 64 | + } |
59 | 65 | }
|
60 | 66 | }
|
0 commit comments