fix(jobs): Add Error Message to JobStatus Failure (#6954)

This commit is contained in:
Diamond Lewis
2020-10-20 16:55:24 -05:00
committed by GitHub
parent 135569cd2b
commit 72428dce0f
2 changed files with 24 additions and 11 deletions

View File

@@ -1652,6 +1652,16 @@ describe('Cloud Code', () => {
); );
}); });
it('should set the failure message on the job error', async () => {
Parse.Cloud.job('myJobError', () => {
throw new Parse.Error(101, 'Something went wrong');
});
const job = await Parse.Cloud.startJob('myJobError');
const jobStatus = await Parse.Cloud.getJobStatus(job);
expect(jobStatus.get('message')).toEqual('Something went wrong');
expect(jobStatus.get('status')).toEqual('failed');
});
function getJobStatus(jobId) { function getJobStatus(jobId) {
const q = new Parse.Query('_JobStatus'); const q = new Parse.Query('_JobStatus');
return q.get(jobId, { useMasterKey: true }); return q.get(jobId, { useMasterKey: true });

View File

@@ -6,7 +6,7 @@ import Auth from './Auth';
const PUSH_STATUS_COLLECTION = '_PushStatus'; const PUSH_STATUS_COLLECTION = '_PushStatus';
const JOB_STATUS_COLLECTION = '_JobStatus'; const JOB_STATUS_COLLECTION = '_JobStatus';
const incrementOp = function(object = {}, key, amount = 1) { const incrementOp = function (object = {}, key, amount = 1) {
if (!object[key]) { if (!object[key]) {
object[key] = { __op: 'Increment', amount: amount }; object[key] = { __op: 'Increment', amount: amount };
} else { } else {
@@ -91,7 +91,7 @@ export function jobStatusHandler(config) {
const objectId = newObjectId(config.objectIdSize); const objectId = newObjectId(config.objectIdSize);
const database = config.database; const database = config.database;
const handler = statusHandler(JOB_STATUS_COLLECTION, database); const handler = statusHandler(JOB_STATUS_COLLECTION, database);
const setRunning = function(jobName, params) { const setRunning = function (jobName, params) {
const now = new Date(); const now = new Date();
jobStatus = { jobStatus = {
objectId, objectId,
@@ -107,27 +107,30 @@ export function jobStatusHandler(config) {
return handler.create(jobStatus); return handler.create(jobStatus);
}; };
const setMessage = function(message) { const setMessage = function (message) {
if (!message || typeof message !== 'string') { if (!message || typeof message !== 'string') {
return Promise.resolve(); return Promise.resolve();
} }
return handler.update({ objectId }, { message }); return handler.update({ objectId }, { message });
}; };
const setSucceeded = function(message) { const setSucceeded = function (message) {
return setFinalStatus('succeeded', message); return setFinalStatus('succeeded', message);
}; };
const setFailed = function(message) { const setFailed = function (message) {
return setFinalStatus('failed', message); return setFinalStatus('failed', message);
}; };
const setFinalStatus = function(status, message = undefined) { const setFinalStatus = function (status, message = undefined) {
const finishedAt = new Date(); const finishedAt = new Date();
const update = { status, finishedAt }; const update = { status, finishedAt };
if (message && typeof message === 'string') { if (message && typeof message === 'string') {
update.message = message; update.message = message;
} }
if (message instanceof Error && typeof message.message === 'string') {
update.message = message.message;
}
return handler.update({ objectId }, update); return handler.update({ objectId }, update);
}; };
@@ -144,7 +147,7 @@ export function pushStatusHandler(config, existingObjectId) {
const database = config.database; const database = config.database;
const handler = restStatusHandler(PUSH_STATUS_COLLECTION, config); const handler = restStatusHandler(PUSH_STATUS_COLLECTION, config);
let objectId = existingObjectId; let objectId = existingObjectId;
const setInitial = function(body = {}, where, options = { source: 'rest' }) { const setInitial = function (body = {}, where, options = { source: 'rest' }) {
const now = new Date(); const now = new Date();
let pushTime = now.toISOString(); let pushTime = now.toISOString();
let status = 'pending'; let status = 'pending';
@@ -193,7 +196,7 @@ export function pushStatusHandler(config, existingObjectId) {
}); });
}; };
const setRunning = function(batches) { const setRunning = function (batches) {
logger.verbose( logger.verbose(
`_PushStatus ${objectId}: sending push to installations with %d batches`, `_PushStatus ${objectId}: sending push to installations with %d batches`,
batches batches
@@ -210,7 +213,7 @@ export function pushStatusHandler(config, existingObjectId) {
); );
}; };
const trackSent = function( const trackSent = function (
results, results,
UTCOffset, UTCOffset,
cleanupInstallations = process.env cleanupInstallations = process.env
@@ -310,7 +313,7 @@ export function pushStatusHandler(config, existingObjectId) {
}); });
}; };
const complete = function() { const complete = function () {
return handler.update( return handler.update(
{ objectId }, { objectId },
{ {
@@ -320,7 +323,7 @@ export function pushStatusHandler(config, existingObjectId) {
); );
}; };
const fail = function(err) { const fail = function (err) {
if (typeof err === 'string') { if (typeof err === 'string') {
err = { message: err }; err = { message: err };
} }