Questions & Answers

How can I customize the error handling and error messages?

Deepkit HTTP
50 up-votes
Warning: The answer is generated by an artificial intelligence. It might not be correct.
To adjust rating, open the thread in Discord and click on the up/down vote button.

To customize error handling and error messages in Deepkit, you can listen to the httpWorkflow.onControllerError event and handle the error there. You can return a custom response with a specific HTTP status code and message. Here's an example:

import { HttpWorkflow, createHttpError, HttpResponse } from '@deepkit/http';

class MyError extends createHttpError(400, 'Custom Error') {}

app.listen(HttpWorkflow.onControllerError, (event) => {
  if (event.error instanceof MyError) {
    const response = new HttpResponse(event.error.message).status(event.error.status);
    event.send(response);
  }
});

In this example, we listen to the HttpWorkflow.onControllerError event and check if the error is an instance of MyError. If it is, we create a custom response with the error message and status code and send it back.

You can customize this logic further based on your specific error handling requirements.