Skip to content

Commit 72428dc

Browse files
authored
fix(jobs): Add Error Message to JobStatus Failure (#6954)
1 parent 135569c commit 72428dc

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

spec/CloudCode.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,16 @@ describe('Cloud Code', () => {
16521652
);
16531653
});
16541654

1655+
it('should set the failure message on the job error', async () => {
1656+
Parse.Cloud.job('myJobError', () => {
1657+
throw new Parse.Error(101, 'Something went wrong');
1658+
});
1659+
const job = await Parse.Cloud.startJob('myJobError');
1660+
const jobStatus = await Parse.Cloud.getJobStatus(job);
1661+
expect(jobStatus.get('message')).toEqual('Something went wrong');
1662+
expect(jobStatus.get('status')).toEqual('failed');
1663+
});
1664+
16551665
function getJobStatus(jobId) {
16561666
const q = new Parse.Query('_JobStatus');
16571667
return q.get(jobId, { useMasterKey: true });

src/StatusHandler.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Auth from './Auth';
66
const PUSH_STATUS_COLLECTION = '_PushStatus';
77
const JOB_STATUS_COLLECTION = '_JobStatus';
88

9-
const incrementOp = function(object = {}, key, amount = 1) {
9+
const incrementOp = function (object = {}, key, amount = 1) {
1010
if (!object[key]) {
1111
object[key] = { __op: 'Increment', amount: amount };
1212
} else {
@@ -91,7 +91,7 @@ export function jobStatusHandler(config) {
9191
const objectId = newObjectId(config.objectIdSize);
9292
const database = config.database;
9393
const handler = statusHandler(JOB_STATUS_COLLECTION, database);
94-
const setRunning = function(jobName, params) {
94+
const setRunning = function (jobName, params) {
9595
const now = new Date();
9696
jobStatus = {
9797
objectId,
@@ -107,27 +107,30 @@ export function jobStatusHandler(config) {
107107
return handler.create(jobStatus);
108108
};
109109

110-
const setMessage = function(message) {
110+
const setMessage = function (message) {
111111
if (!message || typeof message !== 'string') {
112112
return Promise.resolve();
113113
}
114114
return handler.update({ objectId }, { message });
115115
};
116116

117-
const setSucceeded = function(message) {
117+
const setSucceeded = function (message) {
118118
return setFinalStatus('succeeded', message);
119119
};
120120

121-
const setFailed = function(message) {
121+
const setFailed = function (message) {
122122
return setFinalStatus('failed', message);
123123
};
124124

125-
const setFinalStatus = function(status, message = undefined) {
125+
const setFinalStatus = function (status, message = undefined) {
126126
const finishedAt = new Date();
127127
const update = { status, finishedAt };
128128
if (message && typeof message === 'string') {
129129
update.message = message;
130130
}
131+
if (message instanceof Error && typeof message.message === 'string') {
132+
update.message = message.message;
133+
}
131134
return handler.update({ objectId }, update);
132135
};
133136

@@ -144,7 +147,7 @@ export function pushStatusHandler(config, existingObjectId) {
144147
const database = config.database;
145148
const handler = restStatusHandler(PUSH_STATUS_COLLECTION, config);
146149
let objectId = existingObjectId;
147-
const setInitial = function(body = {}, where, options = { source: 'rest' }) {
150+
const setInitial = function (body = {}, where, options = { source: 'rest' }) {
148151
const now = new Date();
149152
let pushTime = now.toISOString();
150153
let status = 'pending';
@@ -193,7 +196,7 @@ export function pushStatusHandler(config, existingObjectId) {
193196
});
194197
};
195198

196-
const setRunning = function(batches) {
199+
const setRunning = function (batches) {
197200
logger.verbose(
198201
`_PushStatus ${objectId}: sending push to installations with %d batches`,
199202
batches
@@ -210,7 +213,7 @@ export function pushStatusHandler(config, existingObjectId) {
210213
);
211214
};
212215

213-
const trackSent = function(
216+
const trackSent = function (
214217
results,
215218
UTCOffset,
216219
cleanupInstallations = process.env
@@ -310,7 +313,7 @@ export function pushStatusHandler(config, existingObjectId) {
310313
});
311314
};
312315

313-
const complete = function() {
316+
const complete = function () {
314317
return handler.update(
315318
{ objectId },
316319
{
@@ -320,7 +323,7 @@ export function pushStatusHandler(config, existingObjectId) {
320323
);
321324
};
322325

323-
const fail = function(err) {
326+
const fail = function (err) {
324327
if (typeof err === 'string') {
325328
err = { message: err };
326329
}

0 commit comments

Comments
 (0)