(reply, error, cb)
| 28 | } |
| 29 | |
| 30 | function handleError (reply, error, cb) { |
| 31 | reply[kReplyIsRunningOnErrorHook] = false |
| 32 | |
| 33 | const context = reply[kRouteContext] |
| 34 | if (reply[kReplyNextErrorHandler] === false) { |
| 35 | fallbackErrorHandler(error, reply, function (reply, payload) { |
| 36 | try { |
| 37 | reply.raw.writeHead(reply.raw.statusCode, reply[kReplyHeaders]) |
| 38 | } catch (error) { |
| 39 | reply.server[kLogController].writeHeadError(error, reply.request, reply) |
| 40 | reply.raw.writeHead(reply.raw.statusCode) |
| 41 | } |
| 42 | reply.raw.end(payload) |
| 43 | }) |
| 44 | return |
| 45 | } |
| 46 | const errorHandler = reply[kReplyNextErrorHandler] || context.errorHandler |
| 47 | |
| 48 | // In case the error handler throws, we set the next errorHandler so we can error again |
| 49 | reply[kReplyNextErrorHandler] = Object.getPrototypeOf(errorHandler) |
| 50 | |
| 51 | // we need to remove content-type to allow content-type guessing for serialization |
| 52 | delete reply[kReplyHeaders]['content-type'] |
| 53 | delete reply[kReplyHeaders]['content-length'] |
| 54 | |
| 55 | const func = errorHandler.func |
| 56 | |
| 57 | if (!func) { |
| 58 | reply[kReplyNextErrorHandler] = false |
| 59 | fallbackErrorHandler(error, reply, cb) |
| 60 | return |
| 61 | } |
| 62 | |
| 63 | try { |
| 64 | const result = func(error, reply.request, reply) |
| 65 | if (result !== undefined) { |
| 66 | if (result !== null && typeof result.then === 'function') { |
| 67 | const store = reply[kDiagnosticsStore] || null |
| 68 | wrapThenable(result, reply, store) |
| 69 | } else { |
| 70 | reply.send(result) |
| 71 | } |
| 72 | } |
| 73 | } catch (err) { |
| 74 | reply.send(err) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | function defaultErrorHandler (error, request, reply) { |
| 79 | setErrorHeaders(error, reply) |
no test coverage detected