Skip to content

Commit 98b0442

Browse files
committed
Merge pull request #32 from AnyFetch/hydration-error
Send hydration_error on ERR HYDRATING
2 parents 6a80118 + 468ce5b commit 98b0442

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

lib/helpers/hydrater.js

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,25 +185,50 @@ 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+
}
214+
else {
215+
var apiUrl = url.parse(task.callback, false, true);
216+
217+
request(apiUrl.protocol + "//" + apiUrl.host)
218+
.patch(apiUrl.path)
219+
.send({
220+
hydration_error: err.toString()
221+
})
222+
.end(cb);
223+
}
192224
}
193-
else {
194-
task.res.send(changes);
195-
task.next();
225+
], function(internalErr) {
226+
if(internalErr) {
227+
errLogger("INTERNAL ERR", internalErr);
196228
}
197-
}
198229

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);
230+
done(err || internalErr, changes);
231+
});
207232
});
208233
};
209234
};

0 commit comments

Comments
 (0)