diff --git a/src/http/http-error.ts b/src/http/http-error.ts index c9bb457..5f75649 100644 --- a/src/http/http-error.ts +++ b/src/http/http-error.ts @@ -44,6 +44,26 @@ export class HttpError extends Error { this.reason = options.reason; } + /** + * Constructs loggable error representation. + * + * Returns an array containing error message, details, and reason. + */ + toLog(): any[] { + + const report: any[] = [this.message]; + const { details, reason } = this; + + if (details) { + report.push(details); + } + if (reason) { + report.push(reason); + } + + return report; + } + } function httpErrorMessage( diff --git a/src/http/http-listener.ts b/src/http/http-listener.ts index aec2408..e38fbe0 100644 --- a/src/http/http-listener.ts +++ b/src/http/http-listener.ts @@ -266,27 +266,16 @@ function httpErrorHandler, + { request: { method, url }, log, error }: RequestContext, ): void { - const report: any[] = [`[${request.method} ${request.url}]`]; + const prefix = `[${method} ${url}]`; if (error instanceof HttpError) { - report.push(error.message); - - const { details, reason } = error; - - if (details) { - report.push(details); - } - if (reason) { - report.push(reason); - } + log.error(prefix, ...error.toLog()); } else { - report.push(error); + log.error(prefix, error); } - - log.error(...report); } /**