Last steps

The conclusion of this exception filter.

We have everything we need. Back in the catch() method, let's define an httpError and a description using the recently created method. If the httpError is undefined, activate the BaseExceptionFilter.

const { httpError, description } = this.createErrorData(code, detail);

if (!httpError) {
  return super.catch(exception, host);
}

Then, we should obtain the fields from the httpError, and the name and value of the field that caused the error.

const { status, error } = httpError;
const { fieldName, fieldValue } = this.extractMessageData(detail);

Finally, create a meta field to hold all the extra information which may help better understand the exception.

const meta = { description, fieldName, fieldValue, table };

The last step is to setup the response and enable this filter globally, and we're done!

response.status(status).json({
  statusCode: status,
  message: detail,
  error,
  meta,
});

Commit - Creating the database exception filter

Last updated