@@ -54,7 +54,7 @@ module.exports = function(hydraterFunction, logger, errLogger) {
5454 if ( err ) {
5555 err = new restify . BadGatewayError ( 'Error when downloading file ' + task . file_path + ': ' + err ) ;
5656 }
57- cb ( err , res . text ) ;
57+ cb ( err , res && res . text ) ;
5858 } ) ;
5959 }
6060 else {
@@ -65,7 +65,7 @@ module.exports = function(hydraterFunction, logger, errLogger) {
6565 if ( res ) {
6666 fs . writeFile ( path , res , cb ) ;
6767 }
68- else {
68+ else {
6969 cb ( ) ;
7070 }
7171 } ,
@@ -81,15 +81,14 @@ module.exports = function(hydraterFunction, logger, errLogger) {
8181 var cleaner = function ( err , changes ) {
8282 if ( ! cleaner . called ) {
8383 cleaner . called = true ;
84- child . kill ( 'SIGKILL' ) ;
8584 cb ( err , changes ) ;
8685 }
8786 clearTimeout ( timeout ) ;
8887 } ;
8988 cleaner . called = false ;
9089
9190 child . on ( 'error' , function ( exitCode ) {
92- cleaner ( new HydrationError ( "Wild error appeared during spwaning child. Exit code:" + exitCode ) ) ;
91+ cleaner ( new HydrationError ( "Wild error appeared while spawning child. Exit code:" + exitCode ) ) ;
9392 } ) ;
9493
9594 child . stderr . on ( 'readable' , function ( ) {
@@ -108,7 +107,7 @@ module.exports = function(hydraterFunction, logger, errLogger) {
108107
109108 child . on ( 'exit' , function ( errCode ) {
110109 if ( errCode !== 0 ) {
111- cleaner ( new HydrationError ( [ "Child exiting with err code: " + errCode , stdout , stderr ] ) ) ;
110+ cleaner ( new HydrationError ( "Child exiting with err code: " + errCode + stdout + stderr ) ) ;
112111 }
113112 } ) ;
114113
@@ -130,15 +129,16 @@ module.exports = function(hydraterFunction, logger, errLogger) {
130129 } ) ;
131130
132131 child . on ( 'message' , function ( res ) {
132+ var err = res . err
133133 // If the function replied with an "HydrationError", we'll wrap this in a nicely formatted document
134134 // and stop the error from bubbling up.
135- if ( res . err && res . err . _hydrationError === true ) {
135+ if ( err && err . _hydrationError ) {
136136 res . changes = { } ;
137137 res . changes . hydration_errored = true ;
138138 res . changes . hydration_error = res . err . message ;
139- res . err = null ;
139+ err = null ;
140140 }
141- cleaner ( res . err , res . changes ) ;
141+ cleaner ( err , res . changes ) ;
142142
143143 } ) ;
144144
@@ -147,6 +147,7 @@ module.exports = function(hydraterFunction, logger, errLogger) {
147147 var changes = { } ;
148148 changes . hydration_errored = true ;
149149 changes . hydration_error = "Task took too long." ;
150+ child . kill ( 'SIGKILL' ) ;
150151 cleaner ( null , changes ) ;
151152 }
152153 } , process . env . TIMEOUT || 60 * 1000 ) ;
0 commit comments