Skip to content

Commit

Permalink
Add loggable representation to HttpError
Browse files Browse the repository at this point in the history
  • Loading branch information
surol committed Nov 8, 2020
1 parent f773098 commit 47385d9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
20 changes: 20 additions & 0 deletions src/http/http-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
19 changes: 4 additions & 15 deletions src/http/http-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,27 +266,16 @@ function httpErrorHandler<TExt, TRequest extends IncomingMessage, TResponse exte
* @internal
*/
function logHttpError(
{ request, log, error }: RequestContext<HttpMeans & ErrorMeans & LoggerMeans>,
{ request: { method, url }, log, error }: RequestContext<HttpMeans & ErrorMeans & LoggerMeans>,
): 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);
}

/**
Expand Down

0 comments on commit 47385d9

Please sign in to comment.