node.js - How to change the default error output in restify -
is there way can change default error output? i'm going change rest error output:
{ "code": "invalidargumenterror", "message": "blah blah..." }
to:
{ "code": 10001, "message": "blah blah", "extramsg": "blah blah" }
here of ideas:
listen error events.
seems not resterror have emitted events (like notfound, methodnotallowed, versionnotallowed... do). can't catch errors rewrite them.listen event before response data sent.
through official documents , have found nothing relative.modify implementation of resterror class.
it's not approach.
any other ideas?
finally provide customized json formatter want:
var server = restify.createserver( { formatters: { 'application/json': function customizedformatjson( req, res, body ) { // copied restify/lib/formatters/json.js if ( body instanceof error ) { // snoop resterror or httperror, don't rely on // instanceof res.statuscode = body.statuscode || 500; if ( body.body ) { body = { code: 10001, scode: body.body.code, msg: body.body.message }; } else { body = { code: 10001, msg: body.message }; } } else if ( buffer.isbuffer( body ) ) { body = body.tostring( 'base64' ); } var data = json.stringify( body ); res.setheader( 'content-length', buffer.bytelength( data ) ); return data; } } } );
Comments
Post a Comment