1
0
Fork 0

XhrHttpImpl#serveError

master
Mike Gerwitz 2015-05-12 14:24:24 -04:00
parent d4328968e8
commit 8fbd4dd220
2 changed files with 73 additions and 10 deletions

View File

@ -109,10 +109,15 @@ module.exports = Class( 'XhrHttpImpl' )
* Subtypes may override this method to alter the ready state change * Subtypes may override this method to alter the ready state change
* actions taken (e.g. to display progress, handle errors, etc.) If * actions taken (e.g. to display progress, handle errors, etc.) If
* only the HTTP status needs to be checked, subtypes may override * only the HTTP status needs to be checked, subtypes may override
* success/failure determination via `#isSuccessful'. * success/failure determination via `#isSuccessful'. If the error
* response needs to be customized, override `#serveError'.
* *
* @param {XMLHttpRequest} req request to hook * When overriding this method, please either call the parent method or
* @param {function(string)} callback continuation to invoke with response * invoke the aforementioned two methods.
*
* @param {XMLHttpRequest} req request to hook
* @param {function(?Error,string)} callback continuation to invoke with
* response
* *
* @return {undefined} * @return {undefined}
* *
@ -131,13 +136,7 @@ module.exports = Class( 'XhrHttpImpl' )
} }
else if ( !( _self.isSuccessful( req.status ) ) ) else if ( !( _self.isSuccessful( req.status ) ) )
{ {
callback( _self.serveError( req, callback );
Error( req.status + " error from server" ),
{
status: req.status,
data: req.responseText
}
);
return; return;
} }
@ -161,6 +160,38 @@ module.exports = Class( 'XhrHttpImpl' )
'virtual protected isSuccessful': function( status ) 'virtual protected isSuccessful': function( status )
{ {
return ( +status >= 200 ) && ( +status < 300 ); return ( +status >= 200 ) && ( +status < 300 );
},
/**
* Serve an error response
*
* The default behavior is to return an Error and content containing the
* HTTP status and response text.
*
* When overriding this method, keep in mind that it should always
* return an Error for the first argument, or set it to null, indicating
* a success.
*
* This method exposes the original XMLHttpRequest used to make the
* request, so it can be used to perform additional analysis for error
* handling, or to override the error and instead return a successful
* response.
*
* @param {XMLHttpRequest} req request to hook
* @param {function(?Error,string)} callback continuation to invoke with
* response
* @return {undefined}
*/
'virtual protected serveError': function( req, callback )
{
callback(
Error( req.status + " error from server" ),
{
status: req.status,
data: req.responseText
}
);
} }
} ); } );

View File

@ -222,6 +222,38 @@ describe( 'XhrHttpImpl', function()
} ); } );
it( 'allows customizing error', function( done )
{
var _self = this,
chk = {};
var StubXhr = createStubXhr();
StubXhr.prototype.status = 404;
Sut.extend(
{
'override protected serveError': function( req, callback )
{
var e = Error( 'foobunny' );
e.foo = true;
expect( req ).to.be.an.instanceOf( StubXhr );
callback( e, chk );
},
} )( StubXhr )
.requestData( 'http://foo', 'GET', '', function( err, resp )
{
expect( ( err || {} ).foo ).to.be.ok;
expect( resp ).to.equal( chk );
done();
} );
StubXhr.inst.send( '' );
} );
it( 'returns self', function() it( 'returns self', function()
{ {
var sut = Sut( function() {} ), var sut = Sut( function() {} ),