简体   繁体   English

如何在 Node.js 中实例化一个新的 SystemError 对象?

[英]How can I instantiate a new SystemError object in Node.js?

Looking at Node's documentation , SystemError extends the Error class.查看 Node 的文档SystemError扩展了Error类。 However, if you attempt to use SystemError directly, Node will throw a ReferenceError saying that SystemError is undefined.但是,如果您尝试直接使用SystemError ,Node 将抛出一个ReferenceError ,说明SystemError未定义。

Clearly, according to the docs, it is possible to encounter an instance of SystemError , but it seems impossible to recreate such an error in the usual way (eg throw new SystemError(args) ).显然,根据文档,可能会遇到SystemError的实例,但似乎不可能以通常的方式重新创建这样的错误(例如throw new SystemError(args) )。

I would like to test some code which is designed to interact with the specified SystemError API as detailed in the docs, but I have no idea how to recreate a SystemError .我想测试一些旨在与文档中详述的指定SystemError API 交互的代码,但我不知道如何重新创建SystemError Is this even possible?这甚至可能吗?

I think that node.js doesn't put SystemError available in order to avoid anyone (npm packages) to use it.我认为 node.js 没有提供SystemError以避免任何人(npm 包)使用它。 So a SystemError is really a SystemError and not something else.所以SystemError实际上是SystemError而不是别的东西。

If you want to test it, generate the error : Try to read a file that doesn't exist for example.如果要测试它,请生成错误:例如,尝试读取不存在的文件。

An other soluce would be to recreate a lookalike error because you know every property of it.另一个解决方法是重新创建一个相似错误,因为您知道它的每个属性。 But it's a workaround actually.但这实际上是一种解决方法。

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

In the Github https://github.com/DefinitelyTyped/DefinitelyTyped I couldn't find any interface declaration of SystemError .在 Github https://github.com/DefinitelyTyped/DefinitelyTyped我找不到SystemError任何interface声明。

Except custom implementations made by node-fetch , voximplant-websdk and alexa-sdk .除了由node-fetchvoximplant-websdkalexa-sdk制作的自定义实现。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM