Search code examples
node.jserror-handlingmiddleware

What is the difference between next(err) and next(new Error(err)) in nodejs?


Is there any reason to write next(new Error(err)) instead of next(err) in nodejs?


Solution

  • Without having any context I would presume that err is already an object (as it usually is in node), so there's no reason to create a new object to just pass it along.

    You would create a new object in a situation where, well, you wanna create a new error :). As opposed to just passing the existing one along.

    next(new Error('Something went wrong'));