Skip to content

Commit

Permalink
Additional logging and type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarkowsky committed Feb 16, 2024
1 parent 37c39d3 commit 70f5244
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions express-api/src/middleware/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import { ErrorWithCode } from "@/utilities/customErrors/ErrorWithCode";
import { NextFunction, Request, Response } from "express";
import { ErrorWithCode } from '@/utilities/customErrors/ErrorWithCode';
import logger from '@/utilities/winstonLogger';
import { NextFunction, Request, Response } from 'express';

const errorHandler = (
err: string | Error | ErrorWithCode,
req: Request,
res: Response,
next: NextFunction,
) => {
// Is this one of the valid input options?
if (!(typeof err === 'string' || err instanceof Error)) {
const message = `Unknown server error.`;
logger.error(message);
return res.status(500).send(message);
}
// Determine what message and status should be
const message = err instanceof Error ? err.message : err;
const code = err instanceof ErrorWithCode ? err.code : 400;
const code = err instanceof ErrorWithCode ? err.code : 500;
// Report through logger
if (code === 500) {
logger.error(message);
} else {
logger.warn(message);
}
// Return status and message
res.status(code).send(`it's broken: ${message}`);
next();
};
Expand Down

0 comments on commit 70f5244

Please sign in to comment.