Skip to content

Commit

Permalink
[fix] #7, improve error handeling for express errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Type-Style committed Mar 8, 2024
1 parent 24506ac commit 67d8ce0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/middleware/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export function notFound(req: Request, res: Response, next: NextFunction) {
next(error);
}

export function handler(err: Error, req: Request, res: Response<Response.Error>, next: NextFunction) {
const statusCode = res.statusCode !== 200 ? res.statusCode : 500;
export function handler(err: HttpError, req: Request, res: Response<Response.Error>, next: NextFunction) {
let statusCode = res.statusCode;
if (statusCode == 200) {
statusCode = err.statusCode || err.status || 500
}
res.status(statusCode);

let message;
Expand Down
5 changes: 5 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

/* eslint-disable @typescript-eslint/no-unused-vars */

interface HttpError extends Error {
status?: number;
statusCode?: number;
}

namespace RateLimit {
interface obj {
[key: string]: {
Expand Down

0 comments on commit 67d8ce0

Please sign in to comment.