Skip to content
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

Unhandled 'error' event #18680

Closed
DmitryAstafyev opened this issue Feb 9, 2018 · 4 comments
Closed

Unhandled 'error' event #18680

DmitryAstafyev opened this issue Feb 9, 2018 · 4 comments
Labels
question Issues that look for answers.

Comments

@DmitryAstafyev
Copy link

  • Version: 8.6.0
  • Platform: darwin
  • Subsystem:

I'm not sure is a "feature" or "bug", but use case is messy.

const EventEmitter  = require('events').EventEmitter;
const Spawn = require('child_process').spawn;
const EVENTS = {
    ERROR: 'error'
};
class Service extends EventEmitter{

    run(){
        let _spawn = Spawn('badcommand', []).on('error', (error) => {
            console.log(error);
        });
    }
}

(new Service()).run();

Expected result: exit code 0 because error event is handled.

But I want to trigger "my" event also:

const EventEmitter  = require('events').EventEmitter;
const Spawn = require('child_process').spawn;
const EVENTS = {
    ERROR: 'error'
};
class Service extends EventEmitter{

    run(){
        let _spawn = Spawn('badcommand', []).on('error', (error) => {
            this.emit(EVENTS.ERROR, error); //<--- Here I want to trigger error event of my service
        });
    }
}

(new Service()).run();

But now result is: exit code 1 - application is crashed within Unhandled 'error' event

Simple changing of event's name fix it:

const EventEmitter  = require('events').EventEmitter;
const Spawn = require('child_process').spawn;
const EVENTS = {
    ERROR: 'error'
};
class Service extends EventEmitter{

    run(){
        let _spawn = Spawn('badcommand', []).on('error', (error) => {
            this.emit('ServiceError', error); //<--- Here I want to trigger error event of my service
        });
    }
}

(new Service()).run();

And again I have exit code 0 as it should be.

So, is a bug? Or feature? Why I cannot use same naming for events even I don't inherite from spawn.

If it's some kind of specific it would be great to have some more informative logs in console for such cases.

Thanks.

@addaleax addaleax added the question Issues that look for answers. label Feb 9, 2018
@addaleax
Copy link
Member

addaleax commented Feb 9, 2018

There is no error handler on your Service instance, but you’re emitting an error on it, so I would say this is expected behavior and not a bug in Node.

If it's some kind of specific it would be great to have some more informative logs in console for such cases.

Could you give a suggestion of output that you would have found helpful?

Also, I’d suggest moving this discussion to https://github.com/nodejs/help/, I think that’s a better place for it (because it’s not a bug with Node).

@DmitryAstafyev
Copy link
Author

Many thanks for so quick response.
Regarding information. I don't know. I added handler for error event to spawn. And expect that this is enough to be sure, that application will not be exit 1. "My" event error I would like to trigger also, but I don't what to make a user of service listen it. User can listen error event or not.
Maybe, I don't know: something like handler of "error" event is ignored, because... Unhandled 'error' event. Just to find quickly what is a reason.

@addaleax
Copy link
Member

addaleax commented Feb 9, 2018

handler of "error" event is ignored

How would Node know how the error handler on another object relates to the one on your own class?

@DmitryAstafyev
Copy link
Author

That's why I'm asking. The simplest thing for me, it's just do not use same naming of events.
And here a "why" I didn't want to make developer listen error (to leave two ways catch it: within reject and within error event):

const EventEmitter  = require('events').EventEmitter;
const Spawn = require('child_process').spawn;
const EVENTS = {
    ERROR: 'error'
};
class Service extends EventEmitter {

    run(){
        return new Promise((resolve, reject) => {
            let _spawn = Spawn('bad', []).on('error', (error) => {
                console.log('"original error"');
                reject();
                this.emit('error', error);
            });
        });
    }
}
const service = new Service();
service.run().catch((error)=>{
    console.log(error);
});

One again thanks for your response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

2 participants