Re-throwing exception in NodeJS and not losing stack trace

后端 未结 4 1233
一向
一向 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:39

    you can also just continue to throw the error up your try chain. If you want to modify anything do so along the way: before the throw statement in b.

    function a() {
        throw new Error('my message');
    }
    
    function b() {
        try {
            a();
        } catch (e) {
            // add / modify properties here
            throw e;
        }
    }
    
    function c() {
        try {
            b();
        } catch (e) {
            console.log(e);
            document.getElementById('logger').innerHTML = e.stack;
        }
    }
    c();

提交回复
热议问题