Skip to content

Commit

Permalink
General error handling for express routes
Browse files Browse the repository at this point in the history
  • Loading branch information
konzz committed Oct 25, 2021
1 parent efe79ab commit c2cf80c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
14 changes: 2 additions & 12 deletions app/api/search.v2/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ interface UwaziReq<T> extends Request {

type UwaziRes = Omit<Response, 'json'> & { json(data: UwaziResponse): Response };

const captureError = (
callback: (req: Request, res: Response, next: NextFunction) => Promise<void>
) => async (req: Request, res: Response, next: NextFunction) => {
try {
await callback(req, res, next);
} catch (e) {
next(e);
}
};

const searchRoutes = (app: Application) => {
app.get(
'/api/v2/entities',
Expand All @@ -43,7 +33,7 @@ const searchRoutes = (app: Application) => {
query: SearchQuerySchema,
},
}),
captureError(async (req: UwaziReq<SearchQuery>, res: UwaziRes) => {
async (req: UwaziReq<SearchQuery>, res: UwaziRes) => {
const { query, language, url } = req;

const response = await elastic.search({ body: await buildQuery(query, language) });
Expand All @@ -55,7 +45,7 @@ const searchRoutes = (app: Application) => {
first: query.page?.limit ? url : undefined,
},
});
})
}
);
};

Expand Down
1 change: 1 addition & 0 deletions app/api/search.v2/specs/routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { elastic } from 'api/search';

import { testingEnvironment } from 'api/utils/testingEnvironment';
import { searchRoutes } from '../routes';

import {
fixturesTitleSearch,
entity1en,
Expand Down
26 changes: 26 additions & 0 deletions app/api/utils/routesErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable no-param-reassign */
import { Application } from 'express';

const wrapHandler = (originalHandler: any) => async (req: any, res: any, next: any) => {
try {
await originalHandler(req, res, next);
} catch (err) {
next(err);
}
};

const routesErrorHandler = (app: Application) => {
const originalGet = app.get.bind(app);
app.get = (path: any, ...args: any[]) => originalGet(path, ...args.map(wrapHandler));

const originalPost = app.post.bind(app);
app.post = (path: any, ...args: any[]) => originalPost(path, ...args.map(wrapHandler));

const originalDelete = app.delete.bind(app);
app.delete = (path: any, ...args: any[]) => originalDelete(path, ...args.map(wrapHandler));

const originalPut = app.put.bind(app);
app.put = (path: any, ...args: any[]) => originalPut(path, ...args.map(wrapHandler));
};

export { routesErrorHandler };
2 changes: 2 additions & 0 deletions app/api/utils/testingRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Response as SuperTestResponse } from 'supertest';

import errorHandlingMiddleware from 'api/utils/error_handling_middleware';
import languageMiddleware from 'api/utils/languageMiddleware';
import { routesErrorHandler } from 'api/utils/routesErrorHandler';
import { extendSupertest } from './supertestExtensions';

extendSupertest();
Expand All @@ -15,6 +16,7 @@ const setUpApp = (
...customMiddleware: ((req: Request, _es: Response, next: NextFunction) => void)[]
): Application => {
const app: Application = express();
routesErrorHandler(app);
app.use(bodyParser.json());
app.use((req: Request, _res: Response, next: NextFunction) => {
req.emitToSessionSocket = (event: string, ...args: any[]) => iosocket.emit(event, ...args);
Expand Down
3 changes: 3 additions & 0 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import bodyParser from 'body-parser';
import compression from 'compression';
import express from 'express';

import helmet from 'helmet';
import { Server } from 'http';
import mongoose from 'mongoose';
Expand Down Expand Up @@ -33,10 +34,12 @@ import { staticFilesMiddleware } from './api/utils/staticFilesMiddleware';
import { customUploadsPath, uploadsPath } from './api/files/filesystem';
import { tocService } from './api/toc_generation/tocService';
import { permissionsContext } from './api/permissions/permissionsContext';
import { routesErrorHandler } from './api/utils/routesErrorHandler';

mongoose.Promise = Promise;

const app = express();
routesErrorHandler(app);
app.use(helmet());

const http = Server(app);
Expand Down

0 comments on commit c2cf80c

Please sign in to comment.