UNPKG

968 BPlain TextView Raw
1// Copyright IBM Corp. and LoopBack contributors 2019. All Rights Reserved.
2// Node module: @loopback/testlab
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6import {Request} from 'express';
7import {IncomingMessage} from 'http';
8
9/**
10 * Creates a Logger that logs an Error if the HTTP status code is not expected
11 *
12 * @param expectedStatusCode - HTTP status code that is expected
13 */
14export function createUnexpectedHttpErrorLogger(
15 expectedStatusCode?: number,
16): LogError {
17 return function logUnexpectedHttpError(
18 err: Error,
19 statusCode: number,
20 req: IncomingMessage,
21 ) {
22 if (statusCode === expectedStatusCode) return;
23
24 /* istanbul ignore next */
25 console.error(
26 'Unhandled error in %s %s: %s %s',
27 req.method,
28 req.url,
29 statusCode,
30 err.stack ?? err,
31 );
32 };
33}
34
35type LogError = (err: Error, statusCode: number, request: Request) => void;