Node.js makes handling errors from inside of Promises difficult

For reference, the full source of the code I'm working with can be found here. The exact spot is in core/printer.js around line 60.

So, basic summary: I maintain my own server back end for Node.js because I like the control it gives me. One of the features it has is the ability to "transform" files based on their extensions. Example, files with a .md extension can be passed to a Markdown parser and then output as HTML to the client.

Another way that I use it is by parsing files with a .interface extension as JavaScript files, letting them run whatever code is inside of them, and letting them have direct control over what response is written to the client.

Now, there may be times at which this process must be done asynchronously (for reading files, etc.) so the API uses promises. The issue is that in my .catch rejection handler, I call a function that generates pages automatically, generally for handling errors. It takes a status code and a description. If the status code is above 500 (which the HTTP standard defines as a server error) then it creates an Error object so that the call stack can be traced to find the issue. For some reason though, when I call new Error it breaks the entire process rather than sending the error page to the client. If I comment out the line that calls new Error then everything works fine, but I lose my ability to find out where the error came from.

The function that generates the error page is here

The garden.error function that it references is the code that actually calls new Error. It looks like this:

garden.error = function ( message, ...extra ) {
    let error = new Error( message )
    console.log( '[Error]', message )
    console.log( error.stack )
    console.log( extra )
    return error
}

Note that it doesn't actually throw the error, it just creates it for the call stack.

Does anyone have any idea how to work around this behavior and make this Promise behave a little better?