@@ -185,25 +185,49 @@ module.exports = function(hydraterFunction, logger, errLogger) {
185185 }
186186 }
187187 ] , function handleErrors ( err , changes , res ) {
188- // if long_poll
189- if ( task . long_poll ) {
190- if ( err ) {
191- task . next ( new restify . InvalidContentError ( "ERR hydrating " + ( ( task . file_path ) ? task . file_path : task . document . id ) + err . toString ( ) ) ) ;
188+ async . waterfall ( [
189+ function logError ( cb ) {
190+ if ( err ) {
191+ errLogger ( "ERR hydrating " + ( ( task . file_path ) ? task . file_path : task . document . id ) , err . toString ( ) ) ;
192+ }
193+
194+ if ( res && res . statusCode && res . statusCode !== 204 ) {
195+ errLogger ( "ERR hydrating: server refused data! Code:" + res . statusCode ) ;
196+ }
197+
198+ cb ( null ) ;
199+ } ,
200+ function forwardError ( cb ) {
201+ if ( ! err ) {
202+ if ( task . long_poll ) {
203+ task . res . send ( changes ) ;
204+ task . next ( ) ;
205+ }
206+
207+ return cb ( null ) ;
208+ }
209+
210+ if ( task . long_poll ) {
211+ task . next ( new restify . InvalidContentError ( "ERR hydrating " + ( ( task . file_path ) ? task . file_path : task . document . id ) + err . toString ( ) ) ) ;
212+ cb ( null ) ;
213+ } else {
214+ var apiUrl = url . parse ( task . callback , false , true ) ;
215+
216+ request ( apiUrl . protocol + "//" + apiUrl . host )
217+ . patch ( apiUrl . path )
218+ . send ( {
219+ hydration_error : err . toString ( )
220+ } )
221+ . end ( cb ) ;
222+ }
192223 }
193- else {
194- task . res . send ( changes ) ;
195- task . next ( ) ;
224+ ] , function ( internalErr ) {
225+ if ( internalErr ) {
226+ errLogger ( "ERR" , internalErr ) ;
196227 }
197- }
198228
199- if ( err ) {
200- errLogger ( "ERR hydrating " + ( ( task . file_path ) ? task . file_path : task . document . id ) , err . toString ( ) ) ;
201- }
202-
203- if ( res && res . statusCode && res . statusCode !== 204 ) {
204- errLogger ( "ERR hydrating: server refused data! Code:" + res . statusCode ) ;
205- }
206- done ( err , changes ) ;
229+ done ( err || internalErr , changes ) ;
230+ } ) ;
207231 } ) ;
208232 } ;
209233} ;
0 commit comments