如何在JavaScript中链接异常(即在java中添加原因) [英] How to chain exceptions in javascript (ie add cause like in java)

查看:86
本文介绍了如何在JavaScript中链接异常(即在java中添加原因)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个标准/最佳实践方式来添加一个异常的原因在JavaScript。在java中,你可以这样做:

  Throwable t = new Exception(whatever); 
t.addCause(priorCaughtException);
throw t;

当打印结果异常时,它会给你一个很好的跟踪,包括原因。在javascript中有什么好办法吗?或者我必须自己滚动?

解决方案

在prod中,我们使用< a href =https://github.com/bluejamesbond/TraceError.js =nofollow> TraceError



用法



 从trace-error导入TraceError; 

global.TraceError = TraceError; //全局公开(可选)

抛出新的TraceError('无法设置状态',srcError,... otherErrors);



输出




Is there a standard / best practice way to add a cause of an exception in javascript. In java you might do this:

Throwable t = new Exception("whatever");
t.addCause(previouslyCaughtException);
throw t;

When the resulting exception is printed, it'll give you a nice trace that includes causes. Is there any good way to do this in javascript or do I have to roll my own?

解决方案

In prod, we use TraceError

Usage

import TraceError from 'trace-error';

global.TraceError = TraceError; // expose globally (optional)

throw new TraceError('Could not set status', srcError, ...otherErrors);

Output

这篇关于如何在JavaScript中链接异常(即在java中添加原因)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆