Skip to content

Commit d4e7108

Browse files
authored
fix(error): remove global prototype toJSON (#1642) fixes #1414
1 parent c66c03d commit d4e7108

File tree

7 files changed

+23
-33
lines changed

7 files changed

+23
-33
lines changed

docs/gitbook/guide/jobs/removing-job.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# Removing job
2-
31
Sometimes it is necessary to remove a job. For example, there could be a job that has bad data.
42

53
```typescript

docs/gitbook/guide/jobs/repeatable.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# Repeatable
2-
3-
## Repeatable
4-
51
There is a special type of _meta_ job called **repeatable**. These jobs are special in the sense that even though you only add one job to the queue, they will keep repeating according to a predefined schedule.
62

73
Adding a job with the `repeat` option set will actually do two things immediately: create a Repeatable Job configuration, and schedule a regular delayed job for the job's first run. This first run will be scheduled "on the hour", that is if you create a job that repeats every 15 minutes at 4:07, the job will first run at 4:15, then 4:30, and so on.
@@ -100,7 +96,7 @@ await myQueue.add(
10096
);
10197
```
10298

103-
### Slow repeatable jobs
99+
## Slow repeatable jobs
104100

105101
It is worth to mention the case where the repeatable frequency is greater than the time it takes to process a job.
106102

src/classes/child-processor.ts

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { promisify } from 'util';
22
import { JobJson, ParentCommand, SandboxedJob } from '../interfaces';
3-
import { childSend } from '../utils';
3+
import { childSend, errorToJSON } from '../utils';
44

55
enum ChildStatus {
66
Idle,
@@ -38,7 +38,7 @@ export class ChildProcessor {
3838
this.status = ChildStatus.Errored;
3939
return childSend(process, {
4040
cmd: ParentCommand.InitFailed,
41-
err: <Error>err,
41+
err: errorToJSON(err),
4242
});
4343
}
4444

@@ -65,7 +65,7 @@ export class ChildProcessor {
6565
if (this.status !== ChildStatus.Idle) {
6666
return childSend(process, {
6767
cmd: ParentCommand.Error,
68-
err: new Error('cannot start a not idling child process'),
68+
err: errorToJSON(new Error('cannot start a not idling child process')),
6969
});
7070
}
7171
this.status = ChildStatus.Started;
@@ -80,7 +80,7 @@ export class ChildProcessor {
8080
} catch (err) {
8181
await childSend(process, {
8282
cmd: ParentCommand.Failed,
83-
value: !(<Error>err).message ? new Error(<any>err) : err,
83+
value: errorToJSON(!(<Error>err).message ? new Error(<any>err) : err),
8484
});
8585
} finally {
8686
this.status = ChildStatus.Idle;
@@ -101,24 +101,6 @@ export class ChildProcessor {
101101
}
102102
}
103103

104-
// https://stackoverflow.com/questions/18391212/is-it-not-possible-to-stringify-an-error-using-json-stringify
105-
if (!('toJSON' in Error.prototype)) {
106-
Object.defineProperty(Error.prototype, 'toJSON', {
107-
value: function toJSONByBull() {
108-
const alt: any = {};
109-
const _this = this;
110-
111-
Object.getOwnPropertyNames(_this).forEach(function (key) {
112-
alt[key] = _this[key];
113-
}, this);
114-
115-
return alt;
116-
},
117-
configurable: true,
118-
writable: true,
119-
});
120-
}
121-
122104
/**
123105
* Enhance the given job argument with some functions
124106
* that can be called from the sandboxed job processor.

src/classes/master.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { toString } from 'lodash';
22
import { ChildProcessor } from './child-processor';
33
import { ParentCommand, ChildCommand } from '../interfaces';
4-
import { childSend } from '../utils';
4+
import { childSend, errorToJSON } from '../utils';
55

66
const childProcessor = new ChildProcessor();
77

@@ -31,7 +31,7 @@ process.on('uncaughtException', async (err: Error) => {
3131
}
3232
await childSend(process, {
3333
cmd: ParentCommand.Failed,
34-
value: err,
34+
value: errorToJSON(err),
3535
});
3636

3737
// An uncaughException leaves this process in a potentially undetermined state so

src/interfaces/child-message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import { ParentCommand } from './parent-command';
33
export interface ChildMessage {
44
cmd: ParentCommand;
55
value?: any;
6-
err?: Error;
6+
err?: Record<string, any>;
77
}

src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ export const parseObjectValues = (obj: {
170170
return accumulator;
171171
};
172172

173+
export const errorToJSON = (value: any): Record<string, any> => {
174+
const error: Record<string, any> = {};
175+
176+
Object.getOwnPropertyNames(value).forEach(function (propName: string) {
177+
error[propName] = value[propName];
178+
});
179+
180+
return error;
181+
};
182+
173183
export const WORKER_SUFFIX = '';
174184

175185
export const QUEUE_EVENT_SUFFIX = ':qe';

tests/fixtures/fixture_processor_fail.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
const delay = require('./delay');
88

9+
class TestError extends Error {
10+
metadata = 'metadata';
11+
}
12+
913
module.exports = function (/*job*/) {
1014
return delay(500).then(() => {
11-
throw new Error('Manually failed processor');
15+
throw new TestError('Manually failed processor');
1216
});
1317
};

0 commit comments

Comments
 (0)