Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Update ThundraWrapper.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
İbrahim Gürses committed Sep 4, 2019
1 parent c54ef30 commit a4f5888
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions src/ThundraWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class ThundraWrapper {
}
}

async invoke() {
invoke() {
const beforeInvocationData = {
originalContext: this.originalContext,
originalEvent: this.originalEvent,
Expand All @@ -109,27 +109,38 @@ class ThundraWrapper {

InvocationSupport.setErrorenous(false);

await this.executeHook('before-invocation', beforeInvocationData, false);

this.pluginContext.requestCount += 1;

try {
const result = this.originalFunction.call(
this.originalThis,
this.originalEvent,
this.wrappedContext,
this.wrappedCallback,
);

if (result && result.then !== undefined && typeof result.then === 'function') {
result.then(this.wrappedContext.succeed, this.wrappedContext.fail);
this.executeHook('before-invocation', beforeInvocationData, false).then(() => {
this.pluginContext.requestCount += 1;
try {
const result = this.originalFunction.call(
this.originalThis,
this.originalEvent,
this.wrappedContext,
this.wrappedCallback,
);

if (result && result.then !== undefined && typeof result.then === 'function') {
result.then(this.wrappedContext.succeed, this.wrappedContext.fail);
}
return result;
} catch (error) {
this.report(error, null, () => this.wrappedContext.fail(error));
}
}).catch((error) => {
ThundraLogger.getInstance().debug(error);
// There is an error on "before-invocation" phase
// So skip Thundra wrapping and call original function directly
const result = this.originalFunction.call(
this.originalThis,
this.originalEvent,
this.originalContext,
this.originalCallback,
);
if (result && result.then !== undefined && typeof result.then === 'function') {
result.then(this.wrappedContext.succeed, this.wrappedContext.fail);
}

return result;
} catch (error) {
await this.report(error, null, null);
throw error;
}
return result;
});
}

async executeHook(hook: any, data: any, reverse: boolean) {
Expand Down

0 comments on commit a4f5888

Please sign in to comment.