@@ -55,10 +55,11 @@ exports = module.exports = internals.Response = class {
5555
5656 this . _events = null ;
5757 this . _payload = null ; // Readable stream
58- this . _error = null ; // The boom object when created from an error (used for logging)
58+ this . _error = options . error || null ; // The boom object when created from an error (used for logging)
5959 this . _contentType = null ; // Used if no explicit content-type is set and type is known
6060 this . _takeover = false ;
6161 this . _statusCode = false ; // true when code() called
62+ this . _state = this . _error ? 'prepare' : 'init' ; // One of 'init', 'prepare', 'marshall', 'close'
6263
6364 this . _processors = {
6465 marshal : options . marshal ,
@@ -495,6 +496,10 @@ exports = module.exports = internals.Response = class {
495496
496497 _prepare ( ) {
497498
499+ Hoek . assert ( this . _state === 'init' ) ;
500+
501+ this . _state = 'prepare' ;
502+
498503 this . _passThrough ( ) ;
499504
500505 if ( ! this . _processors . prepare ) {
@@ -558,10 +563,14 @@ exports = module.exports = internals.Response = class {
558563
559564 async _marshal ( ) {
560565
561- let source = this . source ;
566+ Hoek . assert ( this . _state === 'prepare' ) ;
567+
568+ this . _state = 'marshall' ;
562569
563570 // Processor marshal
564571
572+ let source = this . source ;
573+
565574 if ( this . _processors . marshal ) {
566575 try {
567576 source = await this . _processors . marshal ( this ) ;
@@ -638,6 +647,12 @@ exports = module.exports = internals.Response = class {
638647
639648 _close ( ) {
640649
650+ if ( this . _state === 'close' ) {
651+ return ;
652+ }
653+
654+ this . _state = 'close' ;
655+
641656 if ( this . _processors . close ) {
642657 try {
643658 this . _processors . close ( this ) ;
0 commit comments