From ca68d12370e6505cd6faaccac817d06d5f095275 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Thu, 28 May 2015 12:07:28 -0400 Subject: [PATCH] XhrHttpImpl no longer modifying response text Status code included with error object --- src/dapi/http/XhrHttpImpl.js | 17 ++++++++--------- test/dapi/http/XhrHttpImplTest.js | 13 ++++++++----- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/dapi/http/XhrHttpImpl.js b/src/dapi/http/XhrHttpImpl.js index 3d9a630..ea0c914 100644 --- a/src/dapi/http/XhrHttpImpl.js +++ b/src/dapi/http/XhrHttpImpl.js @@ -168,8 +168,10 @@ module.exports = Class( 'XhrHttpImpl' ) /** * Serve an error response * - * The default behavior is to return an Error and content containing the - * HTTP status and response text. + * The default behavior is to return an Error with the status code as a + * `status` property, and the original response text as the output + * value; the philosophy here is that we should never modify the output, + * since a certain format may be expected as the result. * * When overriding this method, keep in mind that it should always * return an Error for the first argument, or set it to null, indicating @@ -187,12 +189,9 @@ module.exports = Class( 'XhrHttpImpl' ) */ 'virtual protected serveError': function( req, callback ) { - callback( - Error( req.status + " error from server" ), - { - status: req.status, - data: req.responseText - } - ); + var e = Error( req.status + " error from server" ); + e.status = req.status; + + callback( e, req.responseText ); } } ); diff --git a/test/dapi/http/XhrHttpImplTest.js b/test/dapi/http/XhrHttpImplTest.js index 70e1507..61a26fb 100644 --- a/test/dapi/http/XhrHttpImplTest.js +++ b/test/dapi/http/XhrHttpImplTest.js @@ -136,7 +136,7 @@ describe( 'XhrHttpImpl', function() * This is the default behavior, but can be changed by overriding * the onLoad method. */ - it( 'returns an error to the callback', function( done ) + it( 'returns error to callback with status code', function( done ) { var StubXhr = createStubXhr(); StubXhr.prototype.status = 404; @@ -145,29 +145,32 @@ describe( 'XhrHttpImpl', function() .requestData( 'http://foo', 'GET', '', function( err, _ ) { expect( err ).to.be.instanceOf( Error ); + expect( err.message ).to.contain( StubXhr.prototype.status ); + expect( err.status ).to.equal( + StubXhr.prototype.status + ); + done(); } ); } ); - it( 'returns response text with error code', function( done ) + it( 'returns response text as output', function( done ) { var StubXhr = createStubXhr(), status = 404, reply = 'foobunny'; - StubXhr.prototype.status = status; StubXhr.prototype.responseText = reply; Sut( StubXhr ) .requestData( 'http://foo', 'GET', '', function( _, resp ) { - expect( resp.status ).to.equal( status ); - expect( resp.data ).to.equal( reply ); + expect( resp ).to.equal( reply ); done(); } ); } );