-
-
Save mxriverlynn/c69096c7970ba18ecc3d09969b098f3d to your computer and use it in GitHub Desktop.
custom error with clean stack trace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createError(msg, status){ | |
var err = new Error(msg); | |
err.status = status; | |
// uncomment this next line to get a clean stack trace in node.js | |
// Error.captureStackTrace(err, createError); | |
return err; | |
} | |
var err = createError("test", 500); | |
throw err; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have used a similar pattern in the past to create specific error types: https://github.com/CentralPing/errors/blob/master/errors.js
They can be very useful when employing a universal error handler to determine programmatically how to issue an error response.