(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 | if (!reply.log[kDisableRequestLogging]) { |
| 40 | reply.log.warn( |
| 41 | { req: reply.request, res: reply, err: error }, |
| 42 | error?.message |
| 43 | ) |
| 44 | } |
| 45 | reply.raw.writeHead(reply.raw.statusCode) |
| 46 | } |
| 47 | reply.raw.end(payload) |
| 48 | }) |
| 49 | return |
| 50 | } |
| 51 | const errorHandler = reply[kReplyNextErrorHandler] || context.errorHandler |
| 52 | |
| 53 | // In case the error handler throws, we set the next errorHandler so we can error again |
| 54 | reply[kReplyNextErrorHandler] = Object.getPrototypeOf(errorHandler) |
| 55 | |
| 56 | // we need to remove content-type to allow content-type guessing for serialization |
| 57 | delete reply[kReplyHeaders]['content-type'] |
| 58 | delete reply[kReplyHeaders]['content-length'] |
| 59 | |
| 60 | const func = errorHandler.func |
| 61 | |
| 62 | if (!func) { |
| 63 | reply[kReplyNextErrorHandler] = false |
| 64 | fallbackErrorHandler(error, reply, cb) |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | try { |
| 69 | const result = func(error, reply.request, reply) |
| 70 | if (result !== undefined) { |
| 71 | if (result !== null && typeof result.then === 'function') { |
| 72 | const store = reply[kDiagnosticsStore] || null |
| 73 | wrapThenable(result, reply, store) |
| 74 | } else { |
| 75 | reply.send(result) |
| 76 | } |
| 77 | } |
| 78 | } catch (err) { |
| 79 | reply.send(err) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | function defaultErrorHandler (error, request, reply) { |
| 84 | setErrorHeaders(error, reply) |
no test coverage detected