diff --git a/src/Triggers/Trigger.js b/src/Triggers/Trigger.js index 616dd089b8..aa071be483 100644 --- a/src/Triggers/Trigger.js +++ b/src/Triggers/Trigger.js @@ -1,6 +1,6 @@ import { getTrigger, Types } from "./TriggerStore"; import { maybeRunValidator } from "./Validator"; -import { logTriggerAfterHook, logTriggerErrorBeforeHook } from "./Logger"; +import { logTriggerAfterHook } from "./Logger"; import { toJSONwithObjects, resolveError } from "./Utils"; export function getRequestObject( @@ -57,37 +57,36 @@ export async function maybeRunTrigger( parseObject, originalParseObject, config, - context, - responseObject + context ) { - if (!parseObject) { - return {}; - } + try { + if (!parseObject) { + return {}; + } - const trigger = getTrigger(parseObject.className, triggerType, config.applicationId); - if (!trigger) { - return; - } + const trigger = getTrigger(parseObject.className, triggerType, config.applicationId); + if (!trigger) { + return; + } - const request = getRequestObject( - triggerType, - auth, - parseObject, - originalParseObject, - config, - context - ); + const request = getRequestObject( + triggerType, + auth, + parseObject, + originalParseObject, + config, + context + ); - try { await maybeRunValidator(request, `${triggerType}.${parseObject.className}`, auth); if (request.skipWithMasterKey) { return; } - const promise = trigger(request, responseObject); + const response = await trigger(request); - if ([Types.afterSave, Types.afterDelete, Types.afterLogin].includes(triggerType)) { + if (triggerType === Types.afterSave || triggerType === Types.afterDelete) { logTriggerAfterHook( triggerType, parseObject.className, @@ -97,24 +96,9 @@ export async function maybeRunTrigger( ); } - const response = await Promise.resolve(promise); - const responseData = processTriggerResponse(request, response); - if (triggerType === Types.beforeSave) { - return responseData?.object ?? null; - } - - return responseData; - - } catch (err) { - logTriggerErrorBeforeHook( - triggerType, - parseObject.className, - parseObject.toJSON(), - auth, - err, - config.logLevels.triggerBeforeError - ); - throw resolveError(err, { + return processTriggerResponse(request, response); + } catch (e) { + throw resolveError(e, { code: Parse.Error.SCRIPT_FAILED, message: 'Script failed.', }); diff --git a/src/Triggers/TriggerStore.js b/src/Triggers/TriggerStore.js index ac39fc840f..7cd607051e 100644 --- a/src/Triggers/TriggerStore.js +++ b/src/Triggers/TriggerStore.js @@ -205,4 +205,4 @@ export function getFunctionNames(applicationId) { }; extractFunctionNames(null, store); return functionNames; -} \ No newline at end of file +}