@@ -114,6 +114,77 @@ function makeInstance(constructor, constructorOpt, args) {
114114
115115
116116
117+
118+ /**
119+ * built in bunyan serializer for restify errors. it's more or less the
120+ * standard bunyan serializer with support for the context property. borrows
121+ * liberally from:
122+ * https://github.com/trentm/node-bunyan/blob/master/lib/bunyan.js
123+ * @public
124+ * @function serializer
125+ * @param {Object } err an error object
126+ * @returns {Object } serialized object for bunyan output
127+ */
128+ function serializer ( err ) {
129+
130+ if ( ! err || ! err . stack ) {
131+ return err ;
132+ }
133+
134+ function getSerializedContext ( ex ) {
135+
136+ var ret = '' ;
137+
138+ if ( ex . context && _ . keys ( ex . context ) . length > - 1 ) {
139+ ret += ' (' ;
140+ _ . forEach ( ex . context , function ( val , key ) {
141+ ret += key + '=' + val . toString ( ) + ', ' ;
142+ } ) ;
143+ // remove last comma
144+ ret = ret . slice ( 0 , - 2 ) ;
145+ ret += ')' ;
146+ }
147+
148+ return ret + '\n' ;
149+ }
150+
151+ function getFullErrorStack ( ex ) {
152+ var e = ex ;
153+ var out = '' ;
154+ var first = true ;
155+
156+ do {
157+ if ( first !== true ) {
158+ out += '\nCaused by: ' ;
159+ }
160+
161+ // parse out first new line of stack trace, append context
162+ // there.
163+ var stackString = ( e . stack || e . toString ( ) ) . split ( '\n' ) ;
164+
165+ out += stackString . shift ( ) + getSerializedContext ( e ) ;
166+ out += stackString . join ( '\n' ) ;
167+ e = ( e . cause ) ? e . cause ( ) : null ;
168+ first = false ;
169+ } while ( e ) ;
170+
171+ // remove last new line char
172+ out = out . slice ( 0 , - 2 ) ;
173+
174+ return out ;
175+ }
176+
177+ return {
178+ message : err . message ,
179+ name : err . name ,
180+ stack : getFullErrorStack ( err ) ,
181+ code : err . code ,
182+ signal : err . signal
183+ } ;
184+ }
185+
186+
187+
117188module . exports = _ . assign ( { } , httpErrors , restErrors , {
118189 // export base classes
119190 HttpError : HttpError ,
@@ -125,6 +196,9 @@ module.exports = _.assign({}, httpErrors, restErrors, {
125196
126197 // deprecated method names, how long do we keep these for?
127198 // restify has already been updated, but what about external consumers?
128- codeToHttpError : makeErrFromCode
199+ codeToHttpError : makeErrFromCode ,
200+
201+ // built in bunyan serializer
202+ bunyanSerializer : serializer
129203} ) ;
130204
0 commit comments