#How to catch errors not coming from controllers?
10 messages · Page 1 of 1 (latest)
Good old try/carch with some custom error handling. The least you could do is log the error. You can also use some monitoring tool (like sentry) to get notified on such errors.
That makes sense, sentry is on the backlog already for uncaught exceptions that we might have.
I also have a config service (@magic shale/config) where we use the validation method in the .forRoot. When I throw an error from the validation implementation that error is displayed twice. How can I deal with those exceptions?
This is the error class:
import { ValidationError } from 'class-validator';
import { ApplicationError } from '../../../common/errors/application.error'; // export class ApplicationError extends Error {}
import { flattenValidationErrors } from '../../../common/utils/flatten-validation-errors';
export class ConfigError extends ApplicationError {
name = 'Config Error';
constructor(readonly validationErrors: ValidationError[]) {
const message = flattenValidationErrors(validationErrors).map((validationError) => {
const constraints = Object.entries(validationError.constraints || {})
.map(([key, value]) => ` - ${key}: ${value}`)
.join('\n');
return ` - Configuration property "${validationError.property}" (${JSON.stringify(
validationError.value,
)}) does not satisfy the following constraints:\n${constraints}`;
});
super(`The following configuration errors were found:\n${message.join('\n\n')}`);
}
}
This one stops the whole app, right? That must be dealt with on the deployment level. Like when the app fails to start, you rollback to the latest stable release.
That is correct, this will prevent the app from starting.
Or are you asking how not to display the error twice? To me it seems like a non-issue.
Both actually, its been showing it twice since i started this project, i agree that its a non-issue but would like to tidy it up and know the reason why its shown twice.
The reason for catching the ConfigError is that i want to use a different exit code.
The first one seems like it's a regular console.log and the second seems like it's printed with logger.error. I don't know where that comes from though. It all looks like your custom application errors and I have no insight to that
Turns out using ts-node-dev did that. Running the build with just node changed it.