bullmq: Throw custom error class is not propagated properly to the Queue
Hi, my worker throws a custom error class, I do see the error with the relevant class name & stack trace in the Bull-Dashboard.
But when the failed event triggered on the queue process I get only the errors message field.
https://github.com/taskforcesh/bullmq/blob/master/src/classes/job.ts#L375
class CustomError extends Error {
public code: any;
public details: string[];
constructor(code, message, details) {
super(message);
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
this.code = code;
this.details = details;
}
}
// worker - a separate process
const worker = new Worker(
'queueName',
async (job) => throw new CustomError(4004, 'Message not found', 'more details')
);
// queue - main process
queue = new Queue('queueName');
const job = await queue.add('test', { foo: 'bar' });
try {
const result = await job.waitUntilFinished(queueEvents);
} catch (error) {
// error.message will contain "Message not found"
// here the error doesn't have any fields of the custom error event. (such as name, code)
}
In bull-dashboard I do see the original error object, that means that the info exists in redis.

I hope that the issue is clear, if not, I’m available for more details.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 27 (8 by maintainers)
Commits related to this issue
- fix: resolve https://github.com/taskforcesh/bullmq/issues/254 by partially get the error message — committed to younes101020/delivery by deleted user 2 years ago
This is causing a problem for me as well because I have custom types that extend Error with additional fields and that data is lost when I call job.getChildrenValues().
It’s stuff like a user friendly message, fields that failed validation, a process or request id that the error was associated with.
I used to handle this with the worker failed event, which allowed access to the full object thrown by the job, but since re-working things to use the Flows feature, I have to now stuff all information into the Error.message field and parse it back out if the parent job wants to bubble those details out to the job initiator when all of the children have completed.
I know there’s some talk about custom serialization above but I would think JSON serialization would work on a specific property and be a little simpler implementation. So for example, bullmq could define a JobError class with a details attribute that a failing job could attach JSON serializable content to and throw.
I will have to think more about it…
If you throw an error with additional fields, you won’t be able to get them (show them).
You must throw an Error, not an arbitrary object.
@mattwynne I am not super positive about this idea but I may change my opinion in the future.
It could be possible to use something like https://github.com/efossas/Javascript-Serializer within Bull to serialize the error (instead of just capturing the
messageandstackproperties) on the Job.Perhaps we could do this in a third property on
Jobso other things (like the UI) that already read themessageandstackcould continue to rely on them?