Skip to content

Commit 3f4c49a

Browse files
author
Amoki
committed
some improvments
1 parent 2571c7c commit 3f4c49a

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

lib/helpers/hydrater.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

test/errors.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ describe('Errors', function() {
9999
it('should be handled gracefully while hydrating', function(done) {
100100
var fakeApi = require('./helpers/fake-api.js')();
101101
fakeApi.patch('/result', function(req, res, next) {
102-
//should
103102
res.send(204);
104103
next();
105104

test/helpers/fake-api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = function() {
1010
fakeApi.use(restify.acceptParser(fakeApi.acceptable));
1111

1212
fakeApi.get('/afile', function(req, res, next) {
13-
fs.createReadStream(__filename, {encoding: 'utf8'}).pipe(res);
13+
fs.createReadStream(__filename).pipe(res);
1414
next();
1515
});
1616
return fakeApi;

test/helpers/hydrater.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe("hydrate()", function() {
9090
hydrate(task, function(err, changes) {
9191
changes.should.have.property('hydration_errored', true);
9292
process.env.TIMEOUT = 60 * 1000;
93-
done();
93+
done(err);
9494
});
9595
});
9696
});

0 commit comments

Comments
 (0)