Skip to content

Commit

Permalink
refactor(error-handler)!: removed logging from the middleware (#3)
Browse files Browse the repository at this point in the history
* refactor(error-handler)!: removed logging from the middleware

* fix(error-handler): removed unused type
  • Loading branch information
CptSchnitz authored Apr 25, 2021
1 parent 10d836d commit 592bbe6
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const app = express();

app.use('/meow', fn);

app.use(getErrorHandlerMiddleware((message) => console.error(message)));
app.use(getErrorHandlerMiddleware());

app.listen(8080, function() {
console.log('server is up');
Expand Down
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { NextFunction, ErrorRequestHandler } from 'express';
import { StatusCodes, getReasonPhrase } from 'http-status-codes';

type LogFunction = (message: string) => void;
export interface HttpError extends Error {
statusCode?: StatusCodes;
status?: StatusCodes;
Expand All @@ -12,15 +11,14 @@ export interface ErrorResponse {
stacktrace?: string;
}

export const getErrorHandlerMiddleware: (log: LogFunction) => ErrorRequestHandler = (log) => {
export const getErrorHandlerMiddleware: () => ErrorRequestHandler = () => {
const mapColoniesErrorExpressHandler: ErrorRequestHandler = (
err: HttpError,
req,
res,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
next: NextFunction
): void => {
log(`${req.method} request to ${req.originalUrl} has failed with error: ${err.message}`);
const errorResponse: ErrorResponse = {
message: err.message,
};
Expand Down
4 changes: 1 addition & 3 deletions tests/middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import * as supertest from 'supertest';
import { getErrorHandlerMiddleware, HttpError } from '../src/index';
describe('#getErrorHandlerMiddleware', function () {
let expressApp: Application;
let logFn: jest.Mock;
let errorFn: jest.Mock;

beforeAll(function () {
logFn = jest.fn();
errorFn = jest.fn();
expressApp = express();
expressApp.use('/avi', errorFn);
expressApp.use(getErrorHandlerMiddleware(logFn));
expressApp.use(getErrorHandlerMiddleware());
});
describe('production', function () {
beforeAll(function () {
Expand Down

0 comments on commit 592bbe6

Please sign in to comment.