dapi.format.JsonResponse response text output value on parse failure
parent
1669f9eeda
commit
e8344e36bb
|
@ -33,7 +33,10 @@ module.exports = Trait( 'JsonResponse' )
|
|||
/**
|
||||
* 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 {function(?Error,*)} callback continuation upon reply
|
||||
|
@ -56,8 +59,11 @@ module.exports = Trait( 'JsonResponse' )
|
|||
}
|
||||
catch ( e )
|
||||
{
|
||||
// parsing failed
|
||||
callback( e, null );
|
||||
// parsing failed; provide response text in addition to
|
||||
// original data so that the caller can handle how they
|
||||
// please
|
||||
callback( e, { text: resp } );
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -55,15 +55,31 @@ describe( 'dapi.format.JsonRepsonse trait', function()
|
|||
} );
|
||||
|
||||
|
||||
it( 'returns error if JSON parsing fails', function( done )
|
||||
describe( 'when JSON parsing fails', function()
|
||||
{
|
||||
_createStubbedDapi( null, 'ERR' )
|
||||
.request( '', function( err, data )
|
||||
{
|
||||
expect( err ).to.be.instanceOf( Error );
|
||||
expect( data ).to.equal( null );
|
||||
done();
|
||||
} );
|
||||
it( 'returns error', function( done )
|
||||
{
|
||||
_createStubbedDapi( null, 'ERR' )
|
||||
.request( '', function( err, data )
|
||||
{
|
||||
expect( err ).to.be.instanceOf( Error );
|
||||
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();
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue