How can I instantiate a new SystemError object in Node.js?
Asked Answered
N

1

5

Looking at Node's documentation, SystemError extends the Error class. However, if you attempt to use SystemError directly, Node will throw a ReferenceError saying that SystemError is undefined.

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 (e.g. 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. Is this even possible?

Nominee answered 3/2, 2020 at 16:25 Comment(1)
I'm pretty certain that you would need to import it with require('errors') or so, unlike the other errors it's not global. But I guess you shouldn't create SystemErrors yourself anyway.Eatton
T
7

I think that node.js doesn't put SystemError available in order to avoid anyone (npm packages) to use it. So a SystemError is really a SystemError and not something else.

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

In the Github https://github.com/DefinitelyTyped/DefinitelyTyped I couldn't find any interface declaration of SystemError.

Except custom implementations made by node-fetch, voximplant-websdk and alexa-sdk.

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

Twyla answered 3/2, 2020 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.