@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.
@@ -416,6 +416,13 @@ export type Txn = (PaymentParams & {
416
416
  } | ((AppCallMethodCall | AppCreateMethodCall | AppUpdateMethodCall) & {
417
417
  type: 'methodCall';
418
418
  });
419
+ /**
420
+ * A function that transforms an error into a new error.
421
+ *
422
+ * In most cases, an ErrorTransformer should first check if it can or should transform the error
423
+ * and return the input error if it cannot or should not transform it.
424
+ */
425
+ export type ErrorTransformer = (error: Error) => Promise<Error>;
419
426
  /** Parameters to create an `TransactionComposer`. */
420
427
  export type TransactionComposerParams = {
421
428
  /** The algod client to use to get suggestedParams and send the transaction group */
@@ -430,9 +437,14 @@ export type TransactionComposerParams = {
430
437
  defaultValidityWindow?: bigint;
431
438
  /** An existing `AppManager` to use to manage app compilation and cache compilation results.
432
439
  *
433
- * If not specified than an ephemeral one will be created.
440
+ * If not specified then an ephemeral one will be created.
434
441
  */
435
442
  appManager?: AppManager;
443
+ /**
444
+ * An array of error transformers to use when an error is caught in simulate or execute
445
+ * callbacks can later be registered with `registerErrorTransformer`
446
+ */
447
+ errorTransformers?: ErrorTransformer[];
436
448
  };
437
449
  /** Set of transactions built by `TransactionComposer`. */
438
450
  export interface BuiltTransactions {
@@ -466,12 +478,20 @@ export declare class TransactionComposer {
466
478
  /** Whether the validity window was explicitly set on construction */
467
479
  private defaultValidityWindowIsExplicit;
468
480
  private appManager;
481
+ private errorTransformers;
482
+ private transformError;
469
483
  /**
470
484
  * Create a `TransactionComposer`.
471
485
  * @param params The configuration for this composer
472
486
  * @returns The `TransactionComposer` instance
473
487
  */
474
488
  constructor(params: TransactionComposerParams);
489
+ /**
490
+ * Register a function that will be used to transform an error caught when simulating or executing
491
+ *
492
+ * @returns The composer so you can chain method calls
493
+ */
494
+ registerErrorTransformer(transformer: ErrorTransformer): this;
475
495
  /**
476
496
  * Add a pre-built transaction to the transaction group.
477
497
  * @param transaction The pre-built transaction
package/types/composer.js CHANGED
@@ -15,8 +15,37 @@ const address = (address) => {
15
15
  return typeof address === 'string' ? algosdk.Address.fromString(address) : address;
16
16
  };
17
17
  const MAX_TRANSACTION_GROUP_SIZE = 16;
18
+ class InvalidErrorTransformerValue extends Error {
19
+ constructor(originalError, value) {
20
+ super(`An error transformer returned a non-error value: ${value}. The original error before any transformation: ${originalError}`);
21
+ }
22
+ }
23
+ class ErrorTransformerError extends Error {
24
+ constructor(originalError, cause) {
25
+ super(`An error transformer threw an error: ${cause}. The original error before any transformation: ${originalError} `, { cause });
26
+ }
27
+ }
18
28
  /** TransactionComposer helps you compose and execute transactions as a transaction group. */
19
29
  class TransactionComposer {
30
+ async transformError(originalError) {
31
+ // Transformers only work with Error instances, so immediately return anything else
32
+ if (!(originalError instanceof Error)) {
33
+ return originalError;
34
+ }
35
+ let transformedError = originalError;
36
+ for (const transformer of this.errorTransformers) {
37
+ try {
38
+ transformedError = await transformer(transformedError);
39
+ if (!(transformedError instanceof Error)) {
40
+ return new InvalidErrorTransformerValue(originalError, transformedError);
41
+ }
42
+ }
43
+ catch (errorFromTransformer) {
44
+ return new ErrorTransformerError(originalError, errorFromTransformer);
45
+ }
46
+ }
47
+ return transformedError;
48
+ }
20
49
  /**
21
50
  * Create a `TransactionComposer`.
22
51
  * @param params The configuration for this composer
@@ -42,6 +71,16 @@ class TransactionComposer {
42
71
  this.defaultValidityWindow = params.defaultValidityWindow ?? this.defaultValidityWindow;
43
72
  this.defaultValidityWindowIsExplicit = params.defaultValidityWindow !== undefined;
44
73
  this.appManager = params.appManager ?? new types_appManager.AppManager(params.algod);
74
+ this.errorTransformers = params.errorTransformers ?? [];
75
+ }
76
+ /**
77
+ * Register a function that will be used to transform an error caught when simulating or executing
78
+ *
79
+ * @returns The composer so you can chain method calls
80
+ */
81
+ registerErrorTransformer(transformer) {
82
+ this.errorTransformers.push(transformer);
83
+ return this;
45
84
  }
46
85
  /**
47
86
  * Add a pre-built transaction to the transaction group.
@@ -1311,19 +1350,24 @@ class TransactionComposer {
1311
1350
  const { firstValid: firstRound } = suggestedParams;
1312
1351
  waitRounds = Number(BigInt(lastRound) - BigInt(firstRound)) + 1;
1313
1352
  }
1314
- return await transaction.sendAtomicTransactionComposer({
1315
- atc: this.atc,
1316
- suppressLog: params?.suppressLog,
1317
- maxRoundsToWaitForConfirmation: waitRounds,
1318
- populateAppCallResources: params?.populateAppCallResources,
1319
- coverAppCallInnerTransactionFees: params?.coverAppCallInnerTransactionFees,
1320
- additionalAtcContext: params?.coverAppCallInnerTransactionFees
1321
- ? {
1322
- maxFees: this.txnMaxFees,
1323
- suggestedParams: suggestedParams,
1324
- }
1325
- : undefined,
1326
- }, this.algod);
1353
+ try {
1354
+ return await transaction.sendAtomicTransactionComposer({
1355
+ atc: this.atc,
1356
+ suppressLog: params?.suppressLog,
1357
+ maxRoundsToWaitForConfirmation: waitRounds,
1358
+ populateAppCallResources: params?.populateAppCallResources,
1359
+ coverAppCallInnerTransactionFees: params?.coverAppCallInnerTransactionFees,
1360
+ additionalAtcContext: params?.coverAppCallInnerTransactionFees
1361
+ ? {
1362
+ maxFees: this.txnMaxFees,
1363
+ suggestedParams: suggestedParams,
1364
+ }
1365
+ : undefined,
1366
+ }, this.algod);
1367
+ }
1368
+ catch (originalError) {
1369
+ throw await this.transformError(originalError);
1370
+ }
1327
1371
  }
1328
1372
  /**
1329
1373
  * @deprecated Use `send` instead.
@@ -1379,8 +1423,7 @@ class TransactionComposer {
1379
1423
  if (config.Config.debug) {
1380
1424
  await config.Config.events.emitAsync(types_lifecycleEvents.EventType.TxnGroupSimulated, { simulateResponse });
1381
1425
  }
1382
- error.simulateResponse = simulateResponse;
1383
- throw error;
1426
+ throw await this.transformError(error);
1384
1427
  }
1385
1428
  if (config.Config.debug && config.Config.traceAll) {
1386
1429
  await config.Config.events.emitAsync(types_lifecycleEvents.EventType.TxnGroupSimulated, { simulateResponse });