Re-throwing exception in NodeJS and not losing stack trace

后端 未结 4 1234
一向
一向 2021-01-31 01:46

How can I rethrow an error or exception in nodejs/javascript and include a custom message.

I have the following code

var json = JSON.parse(result);
         


        
4条回答
  •  一向
    一向 (楼主)
    2021-01-31 02:20

    You may want to have a look at the verror module from Joyent, which provides an easy way to wrap errors:

    var originError = new Error('No such file or directory');
    var err = new VError(originError, 'Failed to load configuration');
    console.error(err.message);
    

    This will print:

    Failed to load configuration: No such file or directory
    

提交回复
热议问题