如何在NodeJS/Java脚本中重新抛出错误或异常并包含自定义消息。
我有以下代码
var json = JSON.parse(result);
如果出现任何解析错误,我希望在异常消息中包含result
内容。就像这样。
1. try {
2. var json = JSON.parse(result);
3. expect(json.messages.length).to.be(1);
4. } catch(ex) {
5. throw new Error(ex.message + ". " + "JSON response: " + result);
6. }
这里的问题是我丢失了堆栈跟踪。
有没有类似于java
的方法?
throw new Error("JSON response: " + result, ex);
暂无答案!
目前还没有任何答案,快来回答吧!