Skip to content

Commit f1ead58

Browse files
committed
invert try catch position
1 parent 2e90965 commit f1ead58

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

lib/context.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,23 @@ module.exports = class Context {
4848
performance.mark('job_start');
4949
// Defines a timeout for the job
5050
this.startTimeout();
51-
52-
try {
53-
// Wrapps the execution into a setTimeout to be able to abort later
54-
this.execution = setTimeout(() => {
51+
// Wrapps the execution into a setTimeout to be able to abort later
52+
53+
this.execution = setTimeout(() => {
54+
try {
5555
this.running = job(this, this.done);
5656
const isPromise = this.running && typeof this.running.then == 'function';
5757
if(isPromise){
5858
this.running
5959
.then(res => this.done(null, res))
6060
.catch(err => {
61-
console.log('deu erro no job');
6261
this.done(err);
6362
});
6463
}
65-
}, 0);
66-
} catch (error) {
67-
this.done(error);
68-
}
64+
} catch (error) {
65+
this.done(error);
66+
}
67+
}, 0);
6968
}
7069

7170
/**
@@ -80,6 +79,7 @@ module.exports = class Context {
8079
* @returns {Number} duration - Time elapsed from start to stop in ms
8180
*/
8281
stop(){
82+
this.running = undefined;
8383
clearTimeout(this.timeout);
8484
performance.mark('job_stop');
8585
performance.measure('execution_time', 'job_start', 'job_stop');

spec/context.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ describe('Context', () => {
3434
done();
3535
});
3636
});
37+
it('Execute troblesome sync job', (done) => {
38+
ctx.done = (err) => {
39+
ctx.stop();
40+
expect(err).to.exist;
41+
done();
42+
};
43+
44+
ctx.start((ctx, done) => {
45+
throw new Error('Bad job');
46+
});
47+
});
3748
it('Execute resolved job', (done) => {
3849
ctx.done = () => {
3950
ctx.stop();

0 commit comments

Comments
 (0)