Is there an equivalent to Laravel's report helper? #4849
-
https://laravel.com/docs/11.x/errors#the-report-helper I have created a macro for the usual responses that I want from errors: Response.macro('defaultResponse', function (this: Response, data: any, message?: string) {
if (data instanceof Error) {
switch (data.constructor) {
case BusinessLogicException:
this.status(400);
message = data.message;
data = null;
break;
case validationErrors.E_VALIDATION_ERROR:
this.status(422);
message = 'Validation errors occured';
// @ts-ignore
data = data.messages;
break;
case authErrors.E_INVALID_CREDENTIALS:
this.status(401);
message = 'Invalid Email/Password';
data = null;
break;
default:
this.status(500);
message = 'An internal error occured';
if (!app.inProduction) {
data = {
name: data.name,
message: data.message,
stack: data.stack,
};
} else {
data = null;
}
break;
}
}
return {
message: message != null ? message : null,
data: data != null ? data : null,
};
}); However in the default part of the switch I want to pass the error to the HttpExceptionHandler, that way it can both send the error to Sentry and log it into the .log file. While I could just implement this: default:
this.status(500);
message = 'An internal error occured';
logger.error(data.message);
sentry.reportException(data);
if (!app.inProduction) {
data = {
name: data.name,
message: data.message,
stack: data.stack,
};
} else {
data = null;
}
break;
} I would prefer to not fragment exception handling between the HttpExceptionHandler and this macro. Is this possible? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I have no idea how the |
Beta Was this translation helpful? Give feedback.
I see. Right now, there is inbuilt no way to do that. However, you can create a custom function that performs the reporting and use it in both the places, i.e standalone and within the Exception handler.