1
0
Fork 0

dapi.format.JsonResponse response text output value on parse failure

master
Mike Gerwitz 2015-05-28 09:42:00 -04:00
parent 1669f9eeda
commit e8344e36bb
2 changed files with 33 additions and 11 deletions

View File

@ -33,7 +33,10 @@ module.exports = Trait( 'JsonResponse' )
/** /**
* Processes response as JSON * Processes response as JSON
* *
* Will return an error if the response is not valid JSON. * If the response is not valid JSON, an error will be returned. The
* output value will be an object with a single
* property---`text`---containing the response text that failed to
* parse.
* *
* @param {string} data binary data to transmit * @param {string} data binary data to transmit
* @param {function(?Error,*)} callback continuation upon reply * @param {function(?Error,*)} callback continuation upon reply
@ -56,8 +59,11 @@ module.exports = Trait( 'JsonResponse' )
} }
catch ( e ) catch ( e )
{ {
// parsing failed // parsing failed; provide response text in addition to
callback( e, null ); // original data so that the caller can handle how they
// please
callback( e, { text: resp } );
return; return;
} }

View File

@ -55,18 +55,34 @@ describe( 'dapi.format.JsonRepsonse trait', function()
} ); } );
it( 'returns error if JSON parsing fails', function( done ) describe( 'when JSON parsing fails', function()
{
it( 'returns error', function( done )
{ {
_createStubbedDapi( null, 'ERR' ) _createStubbedDapi( null, 'ERR' )
.request( '', function( err, data ) .request( '', function( err, data )
{ {
expect( err ).to.be.instanceOf( Error ); expect( err ).to.be.instanceOf( Error );
expect( data ).to.equal( null );
done(); done();
} ); } );
} ); } );
it( 'provides bad text as object.text', function( done )
{
var text = 'NOT JSON';
_createStubbedDapi( null, text )
.request( '', function( err, data )
{
expect( data ).to.be.a( 'object' );
expect( data.text ).to.equal( text );
done();
} );
} );
} );
it( 'proxy error from encapsulated DataApi', function( done ) it( 'proxy error from encapsulated DataApi', function( done )
{ {
var e = Error( 'foo' ); var e = Error( 'foo' );