HTTP Status code in Command failure handler

29 views
Skip to first unread message

Wolfgang Sommerer

unread,
Oct 31, 2011, 10:15:48 AM10/31/11
to relief-f...@googlegroups.com
Hi,

first and foremost, thanks for this excellent framework.

I'm trying to access the status code of a failed RPC call in the failure handler of a Command object and am unable to find it. Background is, that I want to handle 401s (Unauthorized) differently from other errors.

Any hints how to do this?

Thanks,
W.

Jay Young

unread,
Oct 31, 2011, 3:01:03 PM10/31/11
to relief-f...@googlegroups.com
It's a little round-about, but when your failure callback is executed, it gets passed a goog.net.XhrManager.Event.  This event object has an ".xhrIo" property which is the instance of goog.net.XhrIo.  This object has a getStatus() method.  So it should work like this:

var onFailure = function(e) {
  if (e.xhrIo.getStatus() === 401) {
    // handle Unauthorized failures here.
  }
  ...
};

If that doesn't work, let me know and I'll dig a bit deeper.

Wolfgang Sommerer

unread,
Oct 31, 2011, 3:32:21 PM10/31/11
to relief-f...@googlegroups.com

Thanks for the quick response. I haven't tried the getStatus method but looking at the xhrio status field I always the a 0 value. Also, the getResponseHeaders() of the curious only returns an empty string.

Jay Young

unread,
Oct 31, 2011, 4:42:40 PM10/31/11
to relief-f...@googlegroups.com
Hmmm... I'll take a closer look later tonight and will get back to you.

Jay Young

unread,
Oct 31, 2011, 8:59:29 PM10/31/11
to relief-f...@googlegroups.com
Okay, things actually work a bit differently than I thought.  goog.net.XhrManager dispatches instances of goog.net.XhrManager.Event on itself.  However, when you pass a callback to XhrManager#send(), that callback is handed a regular goog.events.Event instance.  The RPCService uses the second method, so what your onFailure method actually receives is a goog.events.Event with its "target" property set to the XhrIo instance.  So instead of what I wrote above, you could do:

var onFailure = function(e) {
  if (e.target.getStatus() === 401) {
    // handle Unauthorized failures here.
  }
  ...
};

For your other comment, do you mean xhrIo.getResponseHeader('some-header')?  This should work fine, too.  Maybe your server is not setting the header properly for a failed request?  The only thing I can think of is to make sure you are not caching the XhrIo instance and then trying to call methods on it later.  Once your callback returns, the XhrManager disposes the XhrIo and it cannot be accessed again.  If you need specific values, make sure you pull them out inside your callback.

I added a test to rpc_test.js to demonstrate these:  http://code.google.com/p/relief/source/browse/trunk/relief/rpc/rpc_test.js#689 (specifically, see line 702 and 703).  If you're still having trouble, let me know.

Wolfgang Sommerer

unread,
Nov 1, 2011, 2:47:22 AM11/1/11
to relief-f...@googlegroups.com
Ok, tried that but always get 0 as a result of the getStatus() call. Here's the relevant piece of code:
--------------------------------------------------------
goog.provide('videofe.rpc.Command');

goog.require('relief.rpc.Command');
goog.require('videofe.rpc.ServerConfig');



/**
 * @inheritDoc
 */
videofe.rpc.Command = function(onSuccess, onFailure, id, url, method, maxRetries) {
  /**
   * @type {videofe.rpc.ServerConfig}
   * @private
   */
  this.serverConfig_ = videofe.rpc.ServerConfig.getInstance();
  var sc = this.serverConfig_;

  var finalUrl = sc.protocol_ + '://' 
+ sc.host_ 
+ (sc.port_ ? ':' + sc.port_ : '')
+ (sc.urlPrefix_.match("^\/|^$")?'':'/') + sc.urlPrefix_
+ (url.match("^\/")?'':'/') + url;

  var failHandler = function(event) {
 var x = event.target.getStatus();
 onFailure(event);
  }
  goog.base(this, onSuccess, failHandler, id, finalUrl, method, maxRetries);

};
goog.inherits(videofe.rpc.Command, relief.rpc.Command);
-------------------------------------------------------------

I used 'var x = event.target.getAllResponseHeaders();' instead of getStatus() and get an empty string as a result. Tested it both in FF7 and Chrome 15beta.

Thanks for your continued support.

W.

Wolfgang Sommerer

unread,
Nov 1, 2011, 9:23:18 AM11/1/11
to relief-f...@googlegroups.com
So I found the problem. It's been a cross-site scripting issue since I had the server running on another port than plovr. Unfortunately FF doesn't warn about this. Only after I switched to chrome did I see the issue. Sorry for that.

... and thanks for your help.

W.

Jay Young

unread,
Nov 1, 2011, 12:32:02 PM11/1/11
to relief-f...@googlegroups.com
Glad you figured it out!  If you run into any other issues, let me know.  I'm always looking for feedback, especially potential bug reports.
Reply all
Reply to author
Forward
0 new messages