> ## Documentation Index
> Fetch the complete documentation index at: https://docs.privacyboost.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Error handling

# Error Handling

This guide covers error handling patterns for the Privacy Boost WASM SDK. For the cross-platform error code reference, see [Error Handling](/sdk/concepts/error-handling).

## Error Format

The WASM SDK throws standard JavaScript `Error` objects:

```typescript theme={null}
try {
  await sdk.shield({ tokenAddress, amount });
} catch (error) {
  console.log(error.message); // e.g., "Insufficient balance"
}
```

## Common Error Messages

| Error Message | Cause | Retryable |
| - | - | - |
| `"Not connected"` | Wallet not connected | No |
| `"Not authenticated"` | User not logged in | No |
| `"Invalid amount"` | Amount parsing failed | No |
| `"Insufficient balance"` | Not enough tokens | No |
| `"Invalid privacy address"` | Privacy address format wrong | No |
| `"Chain mismatch"` | Wallet on wrong network | No |
| `"Network error: ..."` | Backend unreachable | Yes |

## Basic Error Handling

```typescript theme={null}
try {
  const result = await sdk.shield({ tokenAddress, amount });
} catch (error) {
  if (error.message.includes('Not authenticated')) {
    // User needs to log in
  } else if (error.message.includes('Insufficient balance')) {
    // Not enough tokens
  } else if (error.message.includes('Network error')) {
    // Backend unreachable — retry
  } else if (error.message.includes('Invalid amount')) {
    // Bad input
  } else {
    console.log('Unexpected error:', error.message);
  }
}
```

## Retry Pattern

For network errors, use exponential backoff:

```typescript theme={null}
async function withRetry<T>(
  operation: () => Promise<T>,
  maxRetries: number = 3
): Promise<T> {
  for (let attempt = 1; attempt <= maxRetries; attempt++) {
    try {
      return await operation();
    } catch (error) {
      if (error.message.includes('Network error') && attempt < maxRetries) {
        await new Promise((r) => setTimeout(r, 1000 * attempt));
        continue;
      }
      throw error;
    }
  }
  throw new Error('Unreachable');
}

// Usage
const balance = await withRetry(() => sdk.getBalance(tokenAddress));
```

## User-Friendly Error Messages

```typescript theme={null}
function userMessage(error: Error): string {
  const msg = error.message;

  if (msg.includes('Not connected')) return 'Please connect your wallet';
  if (msg.includes('Not authenticated')) return 'Please log in first';
  if (msg.includes('Insufficient balance')) return 'Not enough balance';
  if (msg.includes('Invalid privacy address')) return 'Invalid recipient address';
  if (msg.includes('Invalid amount')) return 'Invalid amount';
  if (msg.includes('Chain mismatch')) return 'Please switch to the correct network';
  if (msg.includes('Network error')) return 'Network error. Please try again.';

  return 'Something went wrong. Please try again.';
}
```

## Best Practices

### 1. Always Catch Errors

Every SDK call should be wrapped in try/catch:

```typescript theme={null}
try {
  await sdk.shield({ tokenAddress, amount });
} catch (error) {
  console.error('Deposit failed:', error.message);
  showAlert(userMessage(error));
}
```

### 2. Log Errors for Debugging

```typescript theme={null}
try {
  await sdk.shield({ tokenAddress, amount });
} catch (error) {
  console.error('[PrivacyBoost]', error.message);
  showAlert(userMessage(error));
}
```

### 3. Handle Network Errors with Retry

Network errors are transient. Always offer a retry option or use automatic retry with backoff.

## Next Steps

* [Deposits Guide](./deposits)
* [Withdrawals Guide](./withdrawals)
* [Transfers Guide](./transfers)
* [API Reference](/sdk/wasm/api-reference)
* [Error Handling Concepts](/sdk/concepts/error-handling)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.