OK,thanks guys,
what I've posted is trying to simplify the question,
this is the real problem:
Yes indeed, try{...} frame "is" work on the "execution flow (stack trace)",
but that's C, that's C++, that's java!
They're all sync language!
When it comes to nodeJs, shouldn't it be async as well as nodeJs itself?
Let's see a real project:
modules:
httpd.js main module, setup server, dispatch requests.
httpdoc.js helper module, if request is a file (e.g.: /index.htm), the
request will be directed from httpd.js to this module
... other modules
REQUEST => httpd.js =(ANALYSE,DISPATCH)=> httpdoc.js
we should handler all errors inside httpd.js:
file not found: respond 404
access denied: respond 404
file IO failure: respond 500
internal error: respond 500
other responds...
then in httpdocs.js:
fs.ReadStream("...").on("error",function(e){
throw e;
});
how can this error be trapped into httpd.js?
All I can think of is to send the error handler as a param to httpdoc.
What I want is:
httpd.js:
mhttp.Server().on("request",function(req,res){
try{
...analysis,
...dispatch:
var mhttpdocs=require("./httpdocs.js");
mhttpdocs.handleRequest(req,res);
or...
var modulex=require("...some module");
modulex.handleRequest(req,res);
or...
...anything...
}catch(e){
// all errors goes here,
// so we don't have to care about
// if any error happens inside httpdocs,
// or inside modulex that will crash our entire server
}
}).listen(80);
But since nodeJs follows the definition of try{...} after some sync
languages,
rather than to start a new one for async languages like nodeJs itself,
these code won't run like expected.
Could any one come up with a solution?
How to catch exceptions on event callbacks?
Please don't say that you can setup an uncaught exception handler,
there can be only one of it,
I prefer a structured way of catching exceptions,
just like try{...}.
This is really not a problem in C,C++,or Java,
since all code inside modulex is executed synchronizedly,
so any errors will be caught.
But in nodeJs, all IOs seems like to be async,
all is about callbacks, events...
How can I use a structured way to catch them?
在 2013年6月1日星期六UTC+8下午6时09分54秒,revin写道:
>
> Hi,there:
>
> There may be a bug in try{...} frame that
> can't catch exceptions triggered by event listeners inside it.
>
> I'm so interested in nodeJs,
> but since it was young,
> it has a lot of drawbacks.
>
> I've noticed that nodeJs don't have a concurrent api interface,
> so I've decided to write my own framework extension based on nodeJs.
> But soon I've encountered a problem:
> 49:function(req,res){
> 50:try{
> 51: req.on("data",function(data){
> 52: throw new Error("FIXME: DebugTrap");
> 53: }
> 54:}cat1ch(e){}
> 55:}
> I think everyone, of course including me,
> will hope this works,
> but it won't.
>
> The weired thing is,
> when execution reaches line 52,
> it kinds of "inherited" the execution context from line 49,
> see it can access the variables, req and res.
> But the try{...} frame on line 50 can't catch Error on line 52.
>
> It's a V8 problem? Could it be fixed with nodeJs?
> I just don't want to build try{...} frames inside every event listener...
>
--
--
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.