Skip to main content
gave more specific details for error handling when using a Node.js web server framework like Koa/Express.
Source Link
Caleb
  • 913
  • 1
  • 9
  • 7

General Principles of Error Handling

Exceptions should be handled in the layer of code that can actually do something about the error.

The "log and rethrow" pattern is often considered an anipattern antipattern (for exactly the reason you mentioned, leads to a lot of duplicate code and doesn't really help you do anything practical.)

The point of an exception is that it is "not expected". If the layer of code you are working in can't do something reasonable to continue successful processing when the error happens, just let it bubble up.

If the layer of code you are working in can do something to continue when the error happens, that is the spot to handle the error. (And returning a "failed" http response code counts as a way to "continue processing". You are saving the program from crashing.)

Node.js web server error handling (using a library like Koa/Express)

I would strongly recommend using a framework like Koa.js (the "successor" to the very popular Express framework) if you are building an http server in Node.js.

These frameworks give you a very clean and simple toolset to build up your webapi with ease. (and they both give you default error handling out of the box)

Exceptions should be handled in the layer of code that can actually do something about the error.

The "log and rethrow" pattern is often considered an anipattern (for exactly the reason you mentioned, leads to a lot of duplicate code and doesn't really help you do anything practical.)

The point of an exception is that it is "not expected". If the layer of code you are working in can't do something reasonable to continue successful processing when the error happens, just let it bubble up.

If the layer of code you are working in can do something to continue when the error happens, that is the spot to handle the error. (And returning "failed" http response code counts as a way to "continue processing". You are saving the program from crashing.)

General Principles of Error Handling

Exceptions should be handled in the layer of code that can actually do something about the error.

The "log and rethrow" pattern is often considered an antipattern (for exactly the reason you mentioned, leads to a lot of duplicate code and doesn't really help you do anything practical.)

The point of an exception is that it is "not expected". If the layer of code you are working in can't do something reasonable to continue successful processing when the error happens, just let it bubble up.

If the layer of code you are working in can do something to continue when the error happens, that is the spot to handle the error. (And returning a "failed" http response code counts as a way to "continue processing". You are saving the program from crashing.)

Node.js web server error handling (using a library like Koa/Express)

I would strongly recommend using a framework like Koa.js (the "successor" to the very popular Express framework) if you are building an http server in Node.js.

These frameworks give you a very clean and simple toolset to build up your webapi with ease. (and they both give you default error handling out of the box)

Source Link
Caleb
  • 913
  • 1
  • 9
  • 7

Exceptions should be handled in the layer of code that can actually do something about the error.

The "log and rethrow" pattern is often considered an anipattern (for exactly the reason you mentioned, leads to a lot of duplicate code and doesn't really help you do anything practical.)

The point of an exception is that it is "not expected". If the layer of code you are working in can't do something reasonable to continue successful processing when the error happens, just let it bubble up.

If the layer of code you are working in can do something to continue when the error happens, that is the spot to handle the error. (And returning "failed" http response code counts as a way to "continue processing". You are saving the program from crashing.)