-
-
Notifications
You must be signed in to change notification settings - Fork 241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reject on return code > 0 #86
Comments
Have you encountered any problems with it? I don't think we should remove these lines because they are valid and I don't see why we should cater to applications that have bad exit codes. |
Yes, I do encounter problems with users reporting non-zero exit codes when the application ran fine, which is why I'm now forced to suppress exceptions from opn entirely or change this in my fork. And again, there are no "bad exit codes", there is no rule that says an application has to return 0 on success. So again, that leaves me, as someone who doesn't return "bad" exit codes in their own code in the crappy situation that I have to either suppress actual errors as colateral or annoy users. At least make it optional or throw a separate exception type so I can suppress more granular via instanceof? |
We also have a problem with this. Running this code results in: (node:3869) UnhandledPromiseRejectionWarning: Error: Exited with code 3
at ChildProcess.cp.once.code (/home/pi/MagicMirror/modules/MMM-Assistant/node_modules/opn/index.js:84:13)
at Object.onceWrapper (events.js:272:13)
at ChildProcess.emit (events.js:180:13)
at maybeClose (internal/child_process.js:936:16)
at Socket.stream.socket.on (internal/child_process.js:353:11)
at Socket.emit (events.js:180:13)
at Pipe._handle.close [as _onclose] (net.js:538:12)
(node:3869) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
This error originated either by throwing inside of an async function without a catch block,
or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3869) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated.
In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Maybe this could help? |
Is there any workaround for removing these messages? |
@sindresorhus |
If anyone does a good PR to add an option to turn this behavior off, I'll merge it. |
Getting a similar problem when trying to run a vue.js project created with the vue-cli tool using the webpack template. @sindresorhus can you please have a look. Listening at http://localhost:80 (node:8631) UnhandledPromiseRejectionWarning: Error: Exited with code 3 |
We'd love to help, but we are probably not capable of doing so, which is why we reported the issue instead of fixing it. If you are the maintainer and writer of this library, you may want to reconsider, otherwise I'm afraid this could take months, before the right people happen to stop by here and both know how to and want to fix it for you. Thanks in advance. |
@sindresorhus As far as I understood after going through the code, the error is thrown not because of opn, but because of the library which uses it. The code which requires opn, does not handle this rejection. In my case, the error was in the webpack configuration. opn(uri).then((err) => {
console.error(err);
}) As you can clearly see, it does not handle the rejection, hence that warning is shown. If I change the code to: opn(uri).then((res) => {
console.info(res);
}, (err) => {
console.error(err);
}); Then, the warning just goes away. One other solution could be to resolve any exit code instead of resolving only exit code 0 while rejecting others. Change the following code: cp.once('close', code => {
if (code > 0) {
reject(new Error('Exited with code ' + code));
} else {
resolve(cp);
}
}); to cp.once('close', code => {
resolve(cp);
}); |
@dev-z The problem is that the caller can't distinguish between a rejection caused by "error code was > 0" and an "actual" problem so the caller has to either catch all error and ignore them or risk reporting a non-issue to the error log. |
@TanninOne Thank you for explaining it to me. So what should be the pattern/approach followed to avoid such ambiguities? |
Well, there is different approaches, depending on how you see the problem. I come from different programming languages where it's common to throw different exception types to signal different things, so intuitively I'd say: Throw a "NonZeroReturnCode" exception if the return code is not zero and a regular "Error" when it's a node error. However, this is still a hackish solution because it ignores the underlying misconception. The point of opn is to call the user-configured default application so by its nature opn does not know what application it starts and what return codes that application uses to signal what problem (if any). Neither does the caller because if he knew what application he's starting, why use opn in the first place? I just realize: All this is assuming opn actually reports the exit code of the application, not the exit code of the application starter (xdg-open, start, ...) in which case the return code might actually be interesting but would have to be mapped to a common error type such that the caller can handle the error in a platform-independent manner. |
Yeah, we have a use case where we are trying to get an authentication token from the user. They are of course totally unknowing about errors and we'd like to provide some better feedback. |
For anyone that wants to work on this, see previous attempt in #161. |
I have a problem with these lines:
This assumes that an exit code of > 0 signals an error and other exit codes (0 or negative) signal success.
For one thing: Yes, the exit code is a signed integer and convention says everything other than 0 signals an error.
Second: It's purely a convention that an exit code of 0 signals success, not a standard. Depending on the OS and environment it could just as well signal true, false or "nothing has been processed". An exit code of 5 could signal error or it could signal "I have processed 5 items" or "main() had no return because the programming tutorial said the function signature is 'void main()' and 5 is what happened to be on the stack".
Especially on Windows far too many applications don't follow the 0=success convention and since opn opens a default application we can't know if the application that does get run follows convention, so what is the rejection good for
Essentially as soon as windows is one of your target platforms you have to catch and ignore rejection because there is a good chance it rejects for no reason, but thereby also ignore actual errors reported by the error callback.
The text was updated successfully, but these errors were encountered: