Skip to content

Commit

Permalink
fix requestWithError mess
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Dec 18, 2024
1 parent 9224ee0 commit bbb2e95
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
9 changes: 5 additions & 4 deletions src/errors/handleError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextFunction, Request, Response } from "express";
import { uid } from "uid";
import { GroutError } from "./groutError";
import { ErrorType } from "./errorType";
import { reqWithError } from "../logging";
import { RequestWithError } from "../logging";
import { jsonResponseError } from "../jsonResponse";

// We need to include the unused next var for this to be used correctly as an error handler
Expand All @@ -23,9 +23,10 @@ export const handleError = (
: `An unexpected error occurred. Please contact support and quote error code ${uid()}`;

// Set error type, detail and stack on req so morgan logs them
reqWithError(req).errorType = type;
reqWithError(req).errorDetail = detail;
reqWithError(req).errorStack = err.stack;
const reqWithError = req as RequestWithError;
reqWithError.errorType = type;
reqWithError.errorDetail = detail;
reqWithError.errorStack = err.stack;

jsonResponseError(status, type, detail, res);
};
29 changes: 12 additions & 17 deletions src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,20 @@ import { Application, Request, Response } from "express";
import morgan from "morgan";
import { Dict } from "./types/utils";

interface RequestWithError {
errorType: string;
errorDetail: string;
errorStack: string | undefined;
}
export const reqWithError = (req: Request) =>
req as unknown as RequestWithError;
export type RequestWithError = Request & {
errorType?: string;
errorDetail?: string;
errorStack?: string;
};

export const initialiseLogging = (app: Application) => {
// Log error details appended to request by handleError
morgan.token("error-type", (req: Request) => reqWithError(req).errorType);
morgan.token(
"error-detail",
(req: Request) => reqWithError(req).errorDetail
);
morgan.token("error-stack", (req: Request) => reqWithError(req).errorStack);
morgan.token("error-type", (req: RequestWithError) => req.errorType);
morgan.token("error-detail", (req: RequestWithError) => req.errorDetail);
morgan.token("error-stack", (req: RequestWithError) => req.errorStack);

const customFormat = (
tokens: Dict<(req: Request, res: Response, header?: string) => string>,
tokens: Dict<(req: Request, res?: Response, header?: string) => string>,
req: Request,
res: Response
) => {
Expand All @@ -34,9 +29,9 @@ export const initialiseLogging = (app: Application) => {
"-",
tokens["response-time"](req, res),
"ms",
tokens["error-type"](req, res),
tokens["error-detail"](req, res),
tokens["error-stack"](req, res)
tokens["error-type"](req),
tokens["error-detail"](req),
tokens["error-stack"](req)
].join(" ");
};

Expand Down

0 comments on commit bbb2e95

Please sign in to comment.