Skip to content

Commit

Permalink
fix(procedure): call .cleanup on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
monitz87 committed Feb 19, 2021
1 parent ae312ab commit a43adcc
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions src/base/Procedure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,13 @@ export class Procedure<
args: Args,
context: Context
): Promise<ProcedureAuthorizationStatus> {
const status = await this._checkAuthorization(args, context);

this.cleanup();
try {
const status = await this._checkAuthorization(args, context);

return status;
return status;
} finally {
this.cleanup();
}
}

/**
Expand All @@ -190,31 +192,34 @@ export class Procedure<
* @param context - context in which the resulting queue will run
*/
public async prepare(args: Args, context: Context): Promise<TransactionQueue<ReturnValue>> {
await this.setup(args, context);
try {
await this.setup(args, context);

const { roles, permissions } = await this._checkAuthorization(args, context);
const { roles, permissions } = await this._checkAuthorization(args, context);

if (!permissions) {
throw new PolymeshError({
code: ErrorCode.NotAuthorized,
message: "Current Account doesn't have the required permissions to execute this procedure",
});
}

if (!roles) {
throw new PolymeshError({
code: ErrorCode.NotAuthorized,
message: "Current Identity doesn't have the required roles to execute this procedure",
});
}
if (!permissions) {
throw new PolymeshError({
code: ErrorCode.NotAuthorized,
message:
"Current Account doesn't have the required permissions to execute this procedure",
});
}

const returnValue = await this.prepareTransactions(args);
if (!roles) {
throw new PolymeshError({
code: ErrorCode.NotAuthorized,
message: "Current Identity doesn't have the required roles to execute this procedure",
});
}

const transactionQueue = new TransactionQueue(this.transactions, returnValue, context);
const returnValue = await this.prepareTransactions(args);

this.cleanup();
const transactionQueue = new TransactionQueue(this.transactions, returnValue, context);

return transactionQueue;
return transactionQueue;
} finally {
this.cleanup();
}
}

/**
Expand Down

0 comments on commit a43adcc

Please sign in to comment.