Skip to content

Commit

Permalink
add request to logging output in error middleware (#1045)
Browse files Browse the repository at this point in the history
  • Loading branch information
danoswaltCL authored Oct 11, 2023
1 parent d7e802d commit 4663f03
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import { ErrorService } from '../services/ErrorService';
import { ExperimentError } from '../models/ExperimentError';
import { SERVER_ERROR } from 'upgrade_types';

interface ErrorWithRequest extends ExperimentError {
request?: {
[key: string]: any;
};
}

@Middleware({ type: 'after' })
export class ErrorHandlerMiddleware implements ExpressErrorMiddlewareInterface {
public isProduction = env.isProduction;
Expand Down Expand Up @@ -93,7 +99,7 @@ export class ErrorHandlerMiddleware implements ExpressErrorMiddlewareInterface {
break;
case 422:
message = error.message;
type = SERVER_ERROR.UNSUPPORTED_CALIPER
type = SERVER_ERROR.UNSUPPORTED_CALIPER;
break;
default:
message = error.message;
Expand All @@ -103,12 +109,16 @@ export class ErrorHandlerMiddleware implements ExpressErrorMiddlewareInterface {
}

// making error document
const experimentError = new ExperimentError();
const experimentError: ErrorWithRequest = new ExperimentError();
experimentError.name = error.name;
experimentError.message = message;
experimentError.endPoint = req.originalUrl;
experimentError.errorCode = error.httpCode;
experimentError.type = type;

// #1042 send request in logging output, don't need to put into database
experimentError.request = req.body;

req.logger.error(experimentError);
experimentError.type ? await this.errorService.create(experimentError, req.logger) : await Promise.resolve(error);
if (!res.headersSent) {
Expand Down

0 comments on commit 4663f03

Please sign in to comment.