Thanks! I hadn't thought of using setImmediate.

That means my transform stream constructor would become a single call to 
setImmediate, which would then do all the work. Because it's not errors from 
`spawn` so much as it's errors I'd like to emit from the constructor in 
response to incorrect parameters before I even call `spawn`.

It sounds like you would say that in a stream I should emit errors, never throw 
them?


On Jun 17, 2013, at 20:35, Dan Milon wrote:

> You can wrap `spawn` in a `setImmediate`, so that others get a chance to
> attach their events.
> 
> On 06/18/2013 04:19 AM, Ryan Schmidt wrote:
>> Per previous threads, I'm implementing a transform stream around a 
>> particular command line program for use in a web service. How should the 
>> stream report errors?
>> 
>> In the case of the _transform and _flush functions that I'm supposed to 
>> implement, they're given a callback parameter, so I can "callback(new 
>> Error(…))". That's easy, but what about in other situations, like the 
>> constructor?
>> 
>> In the constructor of my custom transform stream, I spawn a process:
>> 
>>  var process = this._process = spawn(options.command, options.args, 
>> options.env);
>> 
>> If the process closes with a non-zero exit code or crashes, I want to be 
>> able to report that, not just to the server's console, but also to the web 
>> browser of the user who made the request that started the process:
>> 
>>  process.on('close', function(code, signal) {
>>    if (signal !== null) {
>>      …
>>    } else if (code !== 0) {
>>      …
>>    }
>>    self.emit('close');
>>  });
>> 
>> If a problem occurred, should I "throw new Error(…)" or "self.emit('error', 
>> new Error(…)"?
>> 
>> I was emitting the error, but am having trouble making that work in code 
>> running directly in the constructor. Let's say I want to allow only a few 
>> different commands ("foo" and "bar") to be run:
>> 
>>  if (!~['foo', 'bar'].indexOf(options.command)) {
>>    this.emit('error', new Error('Invalid command: ' + options.command));
>>  }
>> 
>> If I emit an error like that, then the node program exits saying there was 
>> no listener attached to the stream listening for the error event. But I 
>> cannot attach a listener to the stream until after the constructor has 
>> finished and has returned the stream object to me.
>> 
>> Should I instead throw the error? If so, do I just use a try{}catch{} block 
>> whenever I create such a stream?
>> 
>> 
>> Passing Error objects to a callback is straightforward, but other methods of 
>> error handling in node are still very confusing to me, especially try/catch. 
>> Guidance would be appreciated.
>> 
>> 
> 

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to