# HG changeset patch # User Thinker K.F. Li # Date 1281519459 -28800 # Node ID 32e1b8005403ef22a491e71e522d12de69a01f4a # Parent 6ddc8b42188ff56c40db17ad75765a3a54c5390b Handle exception for event handler diff -r 6ddc8b42188f -r 32e1b8005403 nodejs/observer.cc --- a/nodejs/observer.cc Mon Aug 09 16:43:27 2010 +0800 +++ b/nodejs/observer.cc Wed Aug 11 17:37:39 2010 +0800 @@ -20,6 +20,7 @@ struct xnjsmb_observer_data { Persistent func; + Persistent ctx; }; static void @@ -68,11 +69,14 @@ Handle func) { observer_t *observer; xnjsmb_observer_data *data; + Handle ctx; data = new xnjsmb_observer_data; if(data == NULL) return NULL; data->func = Persistent::New(func); + ctx = Context::GetCurrent(); + data->ctx = Persistent::New(ctx); observer = subject_add_event_observer(subject, type, event_handler, data); @@ -130,11 +134,19 @@ xnjsmb_observer_data *data = (xnjsmb_observer_data *)arg; Handle evt_obj; Handle func_args[1]; + Handle r; + Context::Scope context_scope(data->ctx); + TryCatch trycatch; evt_obj = xnjsmb_auto_event_new(evt); ASSERT(!evt_obj.IsEmpty()); func_args[0] = evt_obj; - data->func->Call(Context::GetCurrent()->Global(), 1, func_args); + r = data->func->Call(data->ctx->Global(), 1, func_args); + if(r.IsEmpty()) { + Handle exception = trycatch.Exception(); + String::AsciiValue exc_str(exception); + fprintf(stderr, "Exception: %s\n", *exc_str); + } } Handle