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
|
* 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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' )
|
it( 'returns error', function( done )
|
||||||
.request( '', function( err, data )
|
{
|
||||||
{
|
_createStubbedDapi( null, 'ERR' )
|
||||||
expect( err ).to.be.instanceOf( Error );
|
.request( '', function( err, data )
|
||||||
expect( data ).to.equal( null );
|
{
|
||||||
done();
|
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