@@ -76,14 +76,16 @@ exports.Boom = class Boom extends Error {
7676 return this . output . statusCode >= 500 ;
7777 }
7878
79+ set isServer ( value ) { } // Allow for compatiblity with legacy boom
80+
7981 constructor ( message , options = { } ) {
8082
8183 const { statusCode = 500 , data, headers, ctor = exports . Boom } = options ;
8284
8385 super ( message ?? internals . codes . get ( statusCode ) ?? 'Unknown' , options ) ;
8486 Error . captureStackTrace ( this , ctor ) ; // Filter the stack to our external API
8587
86- this . # apply( data , statusCode , headers ) ;
88+ internals . apply ( this , data , statusCode , headers ) ;
8789 }
8890
8991 static [ Symbol . hasInstance ] ( instance ) {
@@ -102,53 +104,58 @@ exports.Boom = class Boom extends Error {
102104 this . output . payload = new internals . PayloadObject ( this , this . output . statusCode , debug ) ;
103105 }
104106
105- #apply( data , statusCode , headers , message ) {
107+ static {
108+ Object . defineProperty ( this . prototype , 'name' , { value : 'Boom' , writable : true , configurable : true } ) ;
109+ Object . defineProperty ( this . prototype , 'isBoom' , { value : true , writable : true , configurable : true } ) ;
110+ }
111+ } ;
106112
107- if ( data !== undefined ) {
108- this . data = data ;
109- }
110113
111- if ( statusCode ) {
112- const numberCode = parseInt ( statusCode , 10 ) ;
113- if ( isNaN ( numberCode ) || numberCode < 400 ) {
114- throw new TypeError ( `statusCode must be a number (400+): ${ statusCode } ` ) ;
115- }
114+ exports . isBoom = function ( err , statusCode ) {
116115
117- if ( message ) {
118- this . message = `${ message } : ${ this . message } ` ;
119- }
116+ return err instanceof Error && ! ! err . isBoom && ( ! statusCode || err . output . statusCode === statusCode ) ;
117+ } ;
120118
121- const payload = new internals . PayloadObject ( this , numberCode , false ) ;
122- this . output = new internals . BoomOutput ( numberCode , payload , headers ) ;
123- }
119+
120+ exports . boomify = function ( err , options = { } ) {
121+
122+ const { override, data, statusCode, message } = options ;
123+
124+ if ( ! err ?. isBoom ) {
125+ return new exports . Boom ( message , { statusCode, cause : err , data } ) ;
124126 }
125127
126- static {
127- Object . defineProperty ( this . prototype , 'name' , { value : 'Boom' , writable : true , configurable : true } ) ;
128- Object . defineProperty ( this . prototype , 'isBoom' , { value : true , configurable : true } ) ;
128+ if ( override === false ) { // Defaults to true
129+ internals . apply ( err , data ) ;
130+ }
131+ else {
132+ internals . apply ( err , data , statusCode ?? err . output . statusCode , { } , message ) ;
133+ }
129134
130- exports . isBoom = function ( err , statusCode ) {
135+ err . isServer = err . output . statusCode >= 500 ; // Assign, in case it is a legacy boom object
131136
132- return err instanceof Error && ! ! err . isBoom && ( ! statusCode || err . output . statusCode === statusCode ) ;
133- } ;
137+ return err ;
138+ } ;
134139
135- exports . boomify = function ( err , options = { } ) {
136140
137- const { override , data, statusCode, message } = options ;
141+ internals . apply = function ( boom , data , statusCode , headers , message ) {
138142
139- if ( ! err ?. isBoom ) {
140- return new exports . Boom ( message , { statusCode , cause : err , data } ) ;
141- }
143+ if ( data !== undefined ) {
144+ boom . data = data ;
145+ }
142146
143- if ( override === false ) { // Defaults to true
144- err . #apply( data ) ;
145- }
146- else {
147- err . #apply( data , statusCode ?? err . output . statusCode , { } , message ) ;
148- }
147+ if ( statusCode ) {
148+ const numberCode = parseInt ( statusCode , 10 ) ;
149+ if ( isNaN ( numberCode ) || numberCode < 400 ) {
150+ throw new TypeError ( `statusCode must be a number (400+): ${ statusCode } ` ) ;
151+ }
149152
150- return err ;
151- } ;
153+ if ( message ) {
154+ boom . message = `${ message } : ${ boom . message } ` ;
155+ }
156+
157+ const payload = new internals . PayloadObject ( boom , numberCode , false ) ;
158+ boom . output = new internals . BoomOutput ( numberCode , payload , headers ) ;
152159 }
153160} ;
154161
0 commit comments