@algorandfoundation/algokit-utils 9.0.2-beta.1 → 9.1.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,8 +13,37 @@ const address = (address) => {
13
13
  return typeof address === 'string' ? Address.fromString(address) : address;
14
14
  };
15
15
  const MAX_TRANSACTION_GROUP_SIZE = 16;
16
+ class InvalidErrorTransformerValue extends Error {
17
+ constructor(originalError, value) {
18
+ super(`An error transformer returned a non-error value: ${value}. The original error before any transformation: ${originalError}`);
19
+ }
20
+ }
21
+ class ErrorTransformerError extends Error {
22
+ constructor(originalError, cause) {
23
+ super(`An error transformer threw an error: ${cause}. The original error before any transformation: ${originalError} `, { cause });
24
+ }
25
+ }
16
26
  /** TransactionComposer helps you compose and execute transactions as a transaction group. */
17
27
  class TransactionComposer {
28
+ async transformError(originalError) {
29
+ // Transformers only work with Error instances, so immediately return anything else
30
+ if (!(originalError instanceof Error)) {
31
+ return originalError;
32
+ }
33
+ let transformedError = originalError;
34
+ for (const transformer of this.errorTransformers) {
35
+ try {
36
+ transformedError = await transformer(transformedError);
37
+ if (!(transformedError instanceof Error)) {
38
+ return new InvalidErrorTransformerValue(originalError, transformedError);
39
+ }
40
+ }
41
+ catch (errorFromTransformer) {
42
+ return new ErrorTransformerError(originalError, errorFromTransformer);
43
+ }
44
+ }
45
+ return transformedError;
46
+ }
18
47
  /**
19
48
  * Create a `TransactionComposer`.
20
49
  * @param params The configuration for this composer
@@ -40,6 +69,16 @@ class TransactionComposer {
40
69
  this.defaultValidityWindow = params.defaultValidityWindow ?? this.defaultValidityWindow;
41
70
  this.defaultValidityWindowIsExplicit = params.defaultValidityWindow !== undefined;
42
71
  this.appManager = params.appManager ?? new AppManager(params.algod);
72
+ this.errorTransformers = params.errorTransformers ?? [];
73
+ }
74
+ /**
75
+ * Register a function that will be used to transform an error caught when simulating or executing
76
+ *
77
+ * @returns The composer so you can chain method calls
78
+ */
79
+ registerErrorTransformer(transformer) {
80
+ this.errorTransformers.push(transformer);
81
+ return this;
43
82
  }
44
83
  /**
45
84
  * Add a pre-built transaction to the transaction group.
@@ -1309,19 +1348,24 @@ class TransactionComposer {
1309
1348
  const { firstValid: firstRound } = suggestedParams;
1310
1349
  waitRounds = Number(BigInt(lastRound) - BigInt(firstRound)) + 1;
1311
1350
  }
1312
- return await sendAtomicTransactionComposer({
1313
- atc: this.atc,
1314
- suppressLog: params?.suppressLog,
1315
- maxRoundsToWaitForConfirmation: waitRounds,
1316
- populateAppCallResources: params?.populateAppCallResources,
1317
- coverAppCallInnerTransactionFees: params?.coverAppCallInnerTransactionFees,
1318
- additionalAtcContext: params?.coverAppCallInnerTransactionFees
1319
- ? {
1320
- maxFees: this.txnMaxFees,
1321
- suggestedParams: suggestedParams,
1322
- }
1323
- : undefined,
1324
- }, this.algod);
1351
+ try {
1352
+ return await sendAtomicTransactionComposer({
1353
+ atc: this.atc,
1354
+ suppressLog: params?.suppressLog,
1355
+ maxRoundsToWaitForConfirmation: waitRounds,
1356
+ populateAppCallResources: params?.populateAppCallResources,
1357
+ coverAppCallInnerTransactionFees: params?.coverAppCallInnerTransactionFees,
1358
+ additionalAtcContext: params?.coverAppCallInnerTransactionFees
1359
+ ? {
1360
+ maxFees: this.txnMaxFees,
1361
+ suggestedParams: suggestedParams,
1362
+ }
1363
+ : undefined,
1364
+ }, this.algod);
1365
+ }
1366
+ catch (originalError) {
1367
+ throw await this.transformError(originalError);
1368
+ }
1325
1369
  }
1326
1370
  /**
1327
1371
  * @deprecated Use `send` instead.
@@ -1377,8 +1421,7 @@ class TransactionComposer {
1377
1421
  if (Config.debug) {
1378
1422
  await Config.events.emitAsync(EventType.TxnGroupSimulated, { simulateResponse });
1379
1423
  }
1380
- error.simulateResponse = simulateResponse;
1381
- throw error;
1424
+ throw await this.transformError(error);
1382
1425
  }
1383
1426
  if (Config.debug && Config.traceAll) {
1384
1427
  await Config.events.emitAsync(EventType.TxnGroupSimulated, { simulateResponse });