Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-1743] Platform Inconsistency: HTTPClient's onerror's e.error is not consistently set for Android and iOS

GitHub Issuen/a
TypeBug
PriorityTrivial
StatusClosed
ResolutionInvalid
Resolution Date2012-08-07T19:52:51.000+0000
Affected Version/sRelease 1.7.1
Fix Version/sn/a
ComponentsiOS
Labelsn/a
ReporterJon Alter
AssigneeDon Thorp
Created2011-04-15T03:01:09.000+0000
Updated2012-08-07T19:54:32.000+0000

Description

Problem

On iOS, the HTTPClient's onerror handler e.error returns a generic message and not what the server is really returning, and on Android e.error gets the custom status description. SO to get past this we should fix Android so that it returns the same generic message, and for iOS and Android, move the custom status description in to a new property: e.statusDescription

Reproduction

Drop the following in an app.js. It will display "FAIL:" and tell you if the server's response is not being utilized.
var win = Ti.UI.createWindow({ backgroundColor: '#fff' });
var label = Ti.UI.createLabel({ color: '#000', text: 'Loading...', textAlign: 'center' });
win.add(label);
win.open();

var client = Ti.Network.createHTTPClient({
    onload: function() {
        label.text = 'FAIL! onload handler fired for a request that should have errored.';
    },
    onerror: function(e) {
        if (e.statusDescription != 'This is a custom status description for error code 402!') {
            label.text = 'FAIL! e.statusDescription != the custom status description for error code 402!';
        }
        else {
            label.text = 'PASS! The e.statusDescription was properly set, and the onerror handler was used.'
        }
    }
});
client.open('GET', 'http://appc.me/test/SimulateResponse/?code=402');
client.send();

Workaround

There is no workaround, as there is no way to get at the status description from JavaScript at the moment.

Associated Helpdesk Ticket

http://appc.me/c/XLD-83444-775

Comments

  1. Anirudh Nagesh 2011-06-14

    This error is still reproducible in Titanium 1.7 SDK, iOS 4.3. The link for the sample code is given below. http://www.pastie.org/2069773 - Anirudh Nagesh
  2. Dawson Toth 2011-07-14

    Updated code body and changed reporter to me. Changed customer priority to reflect the pro and enterprise interest in it.
  3. Dawson Toth 2011-08-09

    Updated the example to be significantly more explicit when it fails.
  4. Dawson Toth 2011-08-09

    I'm still seeing this behavior. I updated the example to more clearly draw out the difference between Android and iOS. I also changed this from being a "feature request" -- it's a difference between the platforms that needs to be corrected. Plus, the server is returning something that iOS is ignoring when it constructs the error message.
  5. Dawson Toth 2011-08-09

    Bumped to next release (1.9.0) so that we can flesh out the changes to both platforms that will be necessary.
  6. Don Thorp 2011-10-11

    The error code in the event is not the HTTP error code from the response. It could be null pointer or some other issue. this.status contains the HTTP error code and at least on Android this.statusText contains the description of the error. I propose that a new ticket for statusText be added for iOS and this ticket closed as invalid.
  7. Blain Hamon 2011-10-13

    httpClient's statusText has existed in iOS since August. If anything, this needs documentation.
  8. Don Thorp 2011-10-19

    methods already exist for checking status.
  9. Jon Alter 2011-10-20

    Confirmed working with this.status and this.statusText

    Tested with

    1.8.0.v20111006001414 iOS Simulator 4.3 Android Emulator 2.2
       // Example code:
       var win = Ti.UI.createWindow({
       	backgroundColor : '#fff'
       });
       var label = Ti.UI.createLabel({
       	color : '#000',
       	text : 'Loading...',
       	textAlign : 'center'
       });
       win.add(label);
       win.open();
       
       win.addEventListener('click', function() {
       	var client = Ti.Network.createHTTPClient({
       		onload : function(e) {
       			Ti.API.info('####ONload#####: '+JSON.stringify(e) );
       			Ti.API.info('statusText '+this.statusText);
       			Ti.API.info('status '+this.status);
       			label.text = 'FAIL! onload handler fired for a request that should have errored.';
       		},
       		onerror : function(e) {
       			Ti.API.info('####ONERROR#####: '+JSON.stringify(e) );
       			Ti.API.info('statusText '+this.statusText);
       			Ti.API.info('status '+this.status);
       			if(e.statusDescription != 'This is a custom status description for error code 402!') {
       				label.text = 'FAIL! e.statusDescription != the custom status description for error code 402!';
       			} else {
       				label.text = 'PASS! The e.statusDescription was properly set, and the onerror handler was used.'
       			}
       		}
       	});
       	client.open('GET', 'http://appc.me/test/SimulateResponse/?code=402');
       	// client.setTimeout(1);
       	client.send();
       });
       
       I/TiAPI   (  285): (kroll$1: app://app.js) [2,3610] ####ONERROR#####: {"error":"This is a custom status description for error code 402!"}
       I/TiAPI   (  285): (kroll$1: app://app.js) [2,3612] statusText This is a custom status description for error code 402!
       I/TiAPI   (  285): (kroll$1: app://app.js) [8,3620] status 402
       
       [INFO] ####ONERROR#####: {"source":{},"type":"error"}
       [INFO] statusText HTTP/1.1 402 This is a custom status description for error code 402!
       [INFO] status 402
       

JSON Source