首页 > 解决方案 > 如何在 Node.js 中实例化一个新的 SystemError 对象?

问题描述

查看 Node 的文档SystemError扩展Error类。但是,如果你尝试SystemError直接使用,Node 会抛出一个未定义的ReferenceError说法。SystemError

显然,根据文档,可能会遇到 的实例SystemError,但似乎不可能以通常的方式重新创建这样的错误(例如throw new SystemError(args))。

我想测试一些旨在与SystemError文档中详述的指定 API 交互的代码,但我不知道如何重新创建SystemError. 这甚至可能吗?

标签: javascriptnode.jstypescriptruntime-errorsystem-error

解决方案


我认为 node.js 不可SystemError用是为了避免任何人(npm 包)使用它。所以 aSystemError真的是 aSystemError而不是别的东西。

如果要对其进行测试,请生成错误:例如,尝试读取不存在的文件。

另一个解决方案是重新创建一个相似的错误,因为您知道它的每个属性。但实际上这是一种解决方法。

https://nodejs.org/api/errors.html#errors_class_systemerror

在 Github https://github.com/DefinitelyTyped/DefinitelyTyped找不到任何.interfaceSystemError

除了由node-fetch,voximplant-websdkalexa-sdk.

https://github.com/DefinitelyTyped/DefinitelyTyped/search?q=systemError&unscoped_q=systemError


推荐阅读