Once you wish to develop a backend app utilizing Node.js and Specific as frameworks for server constructing, you have to have a approach to deal with the errors that might happen throughout a service execution.
One approach to stop unhandled errors or non-execution responses is by utilizing the middleware features that Specific permits us to make use of. Right here is an instance of methods to catch and return a server response:

  • As a very good apply normalize ur server response constructions:

Common Http Server Response Interface

Error Http Server Response Interface

  • As soon as u have created interfaces for outline ur server response object u should create lessons thath extends from Error native class for representing attainable server errors

Common Http Error Exception Class

ResourceNotFound Exception Class

All errors in Node.js lengthen from the Error class. This lets you create a customized error class, lengthen from it, and use reserved keys like ‘throw’ for throwing your customized errors. Now, it’s important to create the middleware operate to make use of.

Http Error Middleware

  • After getting your middleware operate outlined it is time to move it to specific

Middleware use setting

To deal with errors in your server software, you might want to use the ‘subsequent’ operate from Specific, as demonstrated under:

Http Error Middleware use

Instance response:

Image description

That is only a approach to deal with attainable server customized errors, this instance had been made with Typescript.

source