catching jsonfx error response

23 views
Skip to first unread message

dishawbr

unread,
Mar 23, 2009, 7:39:44 PM3/23/09
to JsonFx Forum
Hi Stephen,

I'm wondering how I can cactch an error response, ie ErrorResponse
( context, "Request failed. Try reloading the page." ); and do
something with it before my users see the error.

I know that you can do something like
Sys.Net.WebRequestManager.add_completedRequest
(On_WebRequestCompleted); using .net webservices. So I was hoping that
there was an equivalent with JsonFx.

Thanks,
Brian

Stephen McKamey

unread,
Mar 23, 2009, 7:48:18 PM3/23/09
to JsonFx Forum
Hi Brian,

I assume you mean in a JSON-RPC service call, or some other Ajax
request. The service methods all take an options object as their last
parameter whereby you can specify a number of options for context and
callback. The onFailure callback is the one you want.

Here's a breakdown of what is allowed. Most of these are implemented
for you when you are using a JSON-RPC service call:

RequestOptions = {
// HTTP Options
async : bool,
method : string,
headers : Dictionary<string, string>,
timeout : number,
params : string,

// callbacks
onCreate : function(XMLHttpRequest, context){},
onSuccess : function(XMLHttpRequest, context){},
onFailure : function(XMLHttpRequest, context, Error){},
onTimeout : function(XMLHttpRequest, context, Error){},
onComplete : function(XMLHttpRequest, context){},

// callback context
context : object
};

smm

Stephen McKamey

unread,
Mar 23, 2009, 7:52:58 PM3/23/09
to JsonFx Forum
I should have noted that for JSON-RPC service calls, the onSuccess
callback has a signature that has a single parameter matching your
method's return value. And of course the service call itself has a
signature that matches your service method signature with one
additional argument which is this options object.

smm

dishawbr

unread,
Mar 24, 2009, 10:28:50 AM3/24/09
to JsonFx Forum
I'm sorry, I wasn't clear as to where the error was being thrown. I'm
trying to catch errors that are thrown before the ProcessRequest call
is even happening. When an error gets thrown before that call happens,
it seems that the onFailure of the request doesn't work. Maybe I just
have my requests setup incorrectly, but I just wanted to be clear as
to what I'm trying to get.

So how would I use the above call signature to catch an error thrown
like this:

catch ( Exception ex )
{
context.Response.ClearContent();
response.Result = null;
response.Error = new JsonError( ex,
JsonRpcErrors.InternalError );
}

from the JsonServiceHandler? (I took this code right from the
JsonServiceHandler class via Reflector) I would like to show a canned
message to the user instead of something scary.

Thanks,
Brian
> > > Brian- Hide quoted text -
>
> - Show quoted text -

dishawbr

unread,
Mar 24, 2009, 10:55:05 AM3/24/09
to JsonFx Forum
I was able to catch is with onFailure. I guess I didn't have that
function callback defined for one of my calls.

Thanks for the outline of all the avaiable function!

Thanks,
Brian
> > - Show quoted text -- Hide quoted text -

Stephen McKamey

unread,
Mar 24, 2009, 12:04:55 PM3/24/09
to JsonFx Forum
Good, I'm glad that helped.

If you have an application-wide way of handling errors, you can
override the implementation of
JsonFx.IO.onFailure = function(XMLHttpRequest, context, Error){ /
*...*/ };
This way you wouldn't need to specify it for every request.

While we're on the topic, those callbacks are also where you can start/
stop some sort of loading indicator. You can start showing the
indicator in the onCreate callback, then clear it in onComplete. It
will show during the entire duration of the service request, and be
cleared even if there is an error.

smm
Reply all
Reply to author
Forward
0 new messages