onCommandFailure method

  1. @override
Future<void> onCommandFailure(
  1. Command command,
  2. dynamic error
)

Send error responses when command processing fails Only active when aggregate is used as an actor in the actor system

Implementation

@override
Future<void> onCommandFailure(Command command, dynamic error) async {
  await super.onCommandFailure(command, error);

  // Only send responses if we're running in an actor system
  if (!_isInActorSystem()) {
    return;
  }

  // Use captured sender keyed by command ID (same reasoning as onCommandProcessed)
  final sender = _capturedSenders[command.commandId];
  if (sender == null) {
    return;
  }

  final errorMessage = error.toString();

  if (command is CreateWalletCommand) {
    sender.tell(WalletCreatedResponse(
      walletId: command.walletId,
      rootAddress: '',
      success: false,
      error: errorMessage,
    ));
  } else if (command is GenerateAddressCommand) {
    sender.tell(AddressGeneratedResponse(
      walletId: command.walletId,
      address: '',
      derivationIndex: 0,
      success: false,
      error: errorMessage,
      metadata: command.metadata, // Pass through metadata even on error
    ));
  } else if (command is BuildFundingTransactionCommand) {
    sender.tell(FundingTransactionBuiltResponse(
      walletId: command.walletId,
      correlationId: command.correlationId,
      channelId: command.channelId,
      fundingTxHex: '',
      fundingTxId: '',
      fundingOutputIndex: 0,
      success: false,
      error: errorMessage,
    ));
  } else if (command is SignTransactionCommand) {
    sender.tell(TransactionSignedResponse(
      walletId: command.walletId,
      txid: command.transactionId,
      signedHex: '',
      success: false,
      error: errorMessage,
    ));
  } else if (command is SignMultisigTransactionCommand) {
    sender.tell(MultisigTransactionSignedResponse(
      walletId: command.walletId,
      txid: '',
      originalTransactionId: command.transactionId,
      signedHex: '',
      signatureHex: '',
      success: false,
      error: errorMessage,
    ));
  } else if (command is SplitUTXOsToBenfordCommand) {
    sender.tell(SplitUTXOsResponse(
      walletId: command.walletId,
      success: false,
      error: errorMessage,
    ));
  } else {
    // Fallback for any unhandled command - send a generic error response
    sender.tell(LocalMessage(
      payload: {
        'error': errorMessage,
        'command': command.runtimeType.toString(),
      },
    ));
  }
}