mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 15:27:46 +00:00
chore: Split error handler middleware to a seperate file
This commit is contained in:
+4
-25
@@ -1,10 +1,9 @@
|
|||||||
|
|
||||||
import express, { NextFunction, Request, Response } from 'express';
|
import express from 'express';
|
||||||
import helmet from 'helmet';
|
import helmet from 'helmet';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import cookieParser from 'cookie-parser';
|
import cookieParser from 'cookie-parser';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
import * as Sentry from '@sentry/node';
|
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
import { PORT, NODE_ENV, SITE_URL } from './config';
|
import { PORT, NODE_ENV, SITE_URL } from './config';
|
||||||
@@ -30,8 +29,8 @@ import {
|
|||||||
integrationAuth as integrationAuthRouter
|
integrationAuth as integrationAuthRouter
|
||||||
} from './routes';
|
} from './routes';
|
||||||
import { getLogger } from './utils/logger';
|
import { getLogger } from './utils/logger';
|
||||||
import RequestError, { LogLevel } from './utils/requestError';
|
import { catchUnroutedRequests } from './middleware/catchUnroutedRequests';
|
||||||
import { InternalServerError, RouteNotFoundError } from './utils/errors';
|
import { RouteNotFoundError } from './utils/errors';
|
||||||
|
|
||||||
export const app = express();
|
export const app = express();
|
||||||
|
|
||||||
@@ -81,27 +80,7 @@ app.use((req, res, next)=>{
|
|||||||
})
|
})
|
||||||
|
|
||||||
//* Error Handling Middleware (must be after all routing logic)
|
//* Error Handling Middleware (must be after all routing logic)
|
||||||
app.use((error: RequestError|Error, req: Request, res: Response, next: NextFunction)=>{
|
app.use(catchUnroutedRequests)
|
||||||
if(res.headersSent) return next();
|
|
||||||
//TODO: Find better way to type check for error. In current setting you need to cast type to get the functions and variables from RequestError
|
|
||||||
if(!(error instanceof RequestError)){
|
|
||||||
error = InternalServerError({context: {exception: error.message}, stack: error.stack})
|
|
||||||
getLogger('backend-main').log((<RequestError>error).levelName.toLowerCase(), (<RequestError>error).message)
|
|
||||||
}
|
|
||||||
|
|
||||||
//* Set Sentry user identification if req.user is populated
|
|
||||||
if(req.user !== undefined && req.user !== null){
|
|
||||||
Sentry.setUser({ email: req.user.email })
|
|
||||||
}
|
|
||||||
//* Only sent error to Sentry if LogLevel is one of the following level 'ERROR', 'EMERGENCY' or 'CRITICAL'
|
|
||||||
//* with this we will eliminate false-positive errors like 'BadRequestError', 'UnauthorizedRequestError' and so on
|
|
||||||
if([LogLevel.ERROR, LogLevel.EMERGENCY, LogLevel.CRITICAL].includes((<RequestError>error).level)){
|
|
||||||
Sentry.captureException(error)
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status((<RequestError>error).statusCode).json((<RequestError>error).format(req))
|
|
||||||
next()
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
export const server = app.listen(PORT, () => {
|
export const server = app.listen(PORT, () => {
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import { ErrorRequestHandler } from "express";
|
||||||
|
|
||||||
|
import * as Sentry from '@sentry/node';
|
||||||
|
import { InternalServerError } from "../utils/errors";
|
||||||
|
import { getLogger } from "../utils/logger";
|
||||||
|
import RequestError, { LogLevel } from "../utils/requestError";
|
||||||
|
|
||||||
|
|
||||||
|
export const catchUnroutedRequests: ErrorRequestHandler = (error: RequestError|Error, req, res, next) => {
|
||||||
|
if(res.headersSent) return next();
|
||||||
|
//TODO: Find better way to type check for error. In current setting you need to cast type to get the functions and variables from RequestError
|
||||||
|
if(!(error instanceof RequestError)){
|
||||||
|
error = InternalServerError({context: {exception: error.message}, stack: error.stack})
|
||||||
|
getLogger('backend-main').log((<RequestError>error).levelName.toLowerCase(), (<RequestError>error).message)
|
||||||
|
}
|
||||||
|
|
||||||
|
//* Set Sentry user identification if req.user is populated
|
||||||
|
if(req.user !== undefined && req.user !== null){
|
||||||
|
Sentry.setUser({ email: req.user.email })
|
||||||
|
}
|
||||||
|
//* Only sent error to Sentry if LogLevel is one of the following level 'ERROR', 'EMERGENCY' or 'CRITICAL'
|
||||||
|
//* with this we will eliminate false-positive errors like 'BadRequestError', 'UnauthorizedRequestError' and so on
|
||||||
|
if([LogLevel.ERROR, LogLevel.EMERGENCY, LogLevel.CRITICAL].includes((<RequestError>error).level)){
|
||||||
|
Sentry.captureException(error)
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status((<RequestError>error).statusCode).json((<RequestError>error).format(req))
|
||||||
|
next()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user