Skip to content

Insufficient balance from blockchain responds with 500 error but should respond with 402 #2185

@stalniy

Description

@stalniy

What

The API currently returns a 500 Internal Server Error when the blockchain responds with an "insufficient balance" error during transaction simulation. This occurs when attempting to execute and broadcast transactions through the BatchSigningClientService.

Error Details:

Query failed with (6): rpc error: code = Unknown desc = failed to execute message; message index: 1: Deposit invalid: insufficient balance [cosmos/[email protected]/baseapp/baseapp.go:1051] with gas used: '92964': unknown request

Stack Trace:
The error originates from:

  • BatchSigningClientService.estimateFee() during fee estimation (line 356)
  • BatchSigningClientService.signBatch() during batch signing (line 264)
  • SigningStargateClient.simulate() when simulating the transaction

Full Stack Trace:

Error: Query failed with (6): rpc error: code = Unknown desc = failed to execute message; message index: 1: Deposit invalid: insufficient balance [cosmos/[email protected]/baseapp/baseapp.go:1051] with gas used: '92964': unknown request
    at QueryClient.queryAbci (/app/apps/api/node_modules/@cosmjs/stargate/src/queryclient/queryclient.ts:520:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async Object.request (/app/apps/api/node_modules/@cosmjs/stargate/src/queryclient/utils.ts:35:24)
    at async Object.simulate (/app/apps/api/node_modules/@cosmjs/stargate/src/modules/tx/queries.ts:72:26)
    at async SigningStargateClient.simulate (/app/apps/api/node_modules/@cosmjs/stargate/src/signingstargateclient.ts:190:25)
    at async BatchSigningClientService.estimateFee (webpack://akashnetwork/console-api/src/billing/lib/batch-signing-client/batch-signing-client.service.ts:356:27)
    at async BatchSigningClientService.signBatch (webpack://akashnetwork/console-api/src/billing/lib/batch-signing-client/batch-signing-client.service.ts:264:21)
    at async <anonymous> (webpack://akashnetwork/console-api/src/billing/lib/batch-signing-client/batch-signing-client.service.ts:235:27)
    at async withSpan (webpack://akashnetwork/console-api/src/core/services/tracing/tracing.service.ts:169:20)
    at async BatchSigningClientService.executeAndBroadcastBatch (webpack://akashnetwork/console-api/src/billing/lib/batch-signing-client/batch-signing-client.service.ts:234:12)

Why

This is a problem because:

  1. Incorrect HTTP semantics: A 500 status code indicates a server error, but insufficient balance is a client error (user-related issue, not a server malfunction).
  2. Poor client experience: Clients cannot properly distinguish between actual server errors and insufficient balance scenarios.
  3. API best practices: HTTP 402 Payment Required is the semantically correct status code for insufficient balance/payment-related errors.
  4. Debugging difficulty: Mixing client errors with server errors makes monitoring and debugging more challenging.

Suggested Fix

Add error handling in the BatchSigningClientService to catch "insufficient balance" errors and return a 402 status code:

// In BatchSigningClientService.estimateFee() or executeAndBroadcastBatch()
try {
  // Existing simulation/execution logic
  const simulation = await this.signingClient.simulate(address, messages, memo);
  // ... rest of the logic
} catch (error) {
  // Check if error message contains insufficient balance
  if (error.message?.includes("insufficient balance") || 
      error.message?.includes("Deposit invalid")) {
    throw new HttpException(
      {
        message: "Insufficient balance to complete transaction",
        error: "Payment Required"
      },
      HttpStatus.PAYMENT_REQUIRED // 402
    );
  }
  // Re-throw other errors as 500
  throw error;
}

Implementation considerations:

  1. Add the error handler in src/billing/lib/batch-signing-client/batch-signing-client.service.ts around lines 234-356 where the error occurs
  2. Consider creating a custom exception class for payment-related errors (e.g., InsufficientBalanceException)
  3. Ensure proper error logging for monitoring purposes
  4. Update API documentation to reflect the 402 response for insufficient balance scenarios
  5. Add unit tests to verify the error handling behavior

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions