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

# Errors

> Catch and distinguish 4Mica SDK errors, all rooted at FourMicaError.

# Errors

All SDK errors derive from `FourMicaError`. Import the specific classes to branch on failure type.

| Error | When it is raised |
| - | - |
| `ConfigError` | Invalid `FourMica.connect` or `ConfigBuilder` input. |
| `InvalidParamsError` | A caller input or a server response failed validation. Includes `TokenNotSupportedError`, `AmbiguousTokenError`, and `AmountPrecisionError`. |
| `RpcError` | The 4Mica core service returned an error, or the request never arrived. Carries `.status_code`, plus `.code` and `.details` when core attached a coded refusal. |
| `CoreRefusalError` | A coded refusal from core, with its `details` decoded. Both an `RpcError` and a `PaymentError`. See [Coded refusals](#coded-refusals). |
| `ClientError` | Client-side failures: `ClientInitializationError`, `ChainRpcUnavailableError`, a closed session, a remote signer asked to send a transaction. |
| `PaymentError` | The guarantee flow: `SigningError`, `AddressMismatchError`, `SpendLimitError`, the receipt errors below, and `UntrustedCheckpointBatchError`. |
| `ContractError` | An on-chain call failed. A decoded custom error sets `.error_name` and `.error_args`, its arguments keyed by the contract's parameter names. Every revert a deposit, withdrawal, or settlement call can raise is typed after the contract error, such as `AmountZeroError`, `GracePeriodNotElapsedError`, `InsufficientLiquidityError`, `AlreadyPaidError`, `InvalidProofError`, `PaymentWindowElapsedError`, and `AuthorizationExpiredError`. A selector in none of the SDK's ABIs is an `UnknownRevertError`. `RevertedOnChainError` is a transaction that was mined and reverted. |
| `Erc20AllowanceRequiredError` | A self-funded token pull needs an allowance that is not in place. Call the matching `approve()` terminal and retry. |
| `InsufficientBalanceError` / `GasRequiredError` | The wallet does not hold the amount to deposit, or holds no native asset for a transaction it must send itself. Raised before anything is sent. |
| `SponsorshipError` | The gasless routes: `FacilitatorNotConfiguredError`, `FacilitatorRejectedError`, `Permit2AllowanceRequiredError`, `SponsorshipTransportError`, and `OutcomeUnknownError`. |
| `OutcomeUnknownError` | Something was sent and no usable answer came back, so it may have gone through. Do not retry before checking. See [Unknown outcomes](#unknown-outcomes). |
| `X402Error` | An x402 flow error: bad scheme, invalid requirements, or settlement transport. |
| `AuthError` | Base class for all authentication errors: `AuthConfigError`, `AuthUrlError`, `AuthTransportError`, `AuthDecodeError`, and `AuthStatusError`. |
| `AppError` | The app API refused a request or could not be reached. Lives in `fourmica_sdk.app`, with `.status`, `.code`, and `.issues`. |

> [!NOTE]
>
> The package root exports the errors you branch on most often. The rest import
> from `fourmica_sdk.errors`: the `AuthError` subclasses,
> `SponsorshipTransportError`, and contract reverts such as
> `InsufficientAvailableError`, `InsufficientLiquidityError`, and `PausedError`.

## Coded refusals

When core refuses with a stable code, the SDK raises a `CoreRefusalError` subclass carrying the decoded `details`. Each is also an `RpcError`, so handlers written against `RpcError` keep catching them. Branch on the class (or `.code`), not the message.

| Error | `code` | Meaning |
| - | - | - |
| `CheckpointRequiredError` | `CHECKPOINT_REQUIRED` | The payer holds too many guarantees past its latest checkpoint. Submit one and retry. |
| `CheckpointMismatchError` | `CHECKPOINT_MISMATCH` | The checkpoint no longer matches what core holds. Re-read the batch and sign again. |
| `CycleMismatchError` | `CYCLE_MISMATCH` | The payment was signed for a cycle other than the active one. Build and sign again. |
| `FutureTimestampError` | `FUTURE_TIMESTAMP` | The payment is stamped later than core's clock. Build and sign again. |
| `StaleTimestampError` | `STALE_TIMESTAMP` | The payment is stamped before the active cycle opened. Build and sign again. |
| `TimestampNotPastCutoffError` | `TIMESTAMP_NOT_PAST_CUTOFF` | The payment is stamped at or before the payer's latest checkpoint cutoff. Build and sign again. |

The client that receives one of the last four records core's clock and drops the stale cycle. The next payment it builds with `new_payment` is scoped and stamped correctly.

## Unknown outcomes

A transaction that has left the wallet cannot be called back, and an answer that never arrives does not mean it failed. The SDK reports that case as `OutcomeUnknownError`, never as an ordinary failure, so that a retry loop does not send the same money twice.

| Error | When it is raised |
| - | - |
| `OutcomeUnknownError` | A sponsored action reached the facilitator and no usable answer came back. |
| `TransactionOutcomeUnknownError` | A transaction the wallet sent itself. A subclass of `OutcomeUnknownError`, so one `except` clause covers both. |

`TransactionOutcomeUnknownError` says how far the transaction got:

* **`stage == "broadcast"`**: it was handed to the chain's node, and no usable answer came back: a timeout, a dropped connection, a gateway's 5xx, an HTTP 408, or a JSON-RPC internal error (`-32603`). The node may have taken it, and `tx_hash` is `None`.
* **`stage == "receipt"`**: the node accepted it, as `tx_hash`, and its receipt was not seen in time. It may still be mined.

A refusal is not an unknown outcome. Any other JSON-RPC error from the node, any other HTTP 4xx, or a failure before the broadcast stays a `ContractError`: nothing was sent. A `SponsorshipTransportError` means the facilitator never received the request, so it cannot have acted on it.

```python theme={null}
from fourmica_sdk import OutcomeUnknownError, TransactionOutcomeUnknownError

try:
    await client.deposit("5 USDC")
except TransactionOutcomeUnknownError as error:
    if error.tx_hash:
        ...  # Sent as error.tx_hash: look it up before doing anything else.
    else:
        ...  # No hash. Check what it would have changed: client.balance().
except OutcomeUnknownError:
    ...  # The facilitator may have submitted it. Check client.balance().
```

Here `client` is a `FourMica` session.

List the subclass first: Python runs the first `except` clause that matches.

Two results are not exceptions at all. `client.settle()` records an unknown outcome as a result with `status == "unknown"`, and `client.deposit()` returns `status="pending"` when the transaction is mined and core has not counted it yet. Check the first before settling again, and wait on the second with `await result.spendable()`.

> [!WARNING]
>
> A `FacilitatorRejectedError` whose `transaction` attribute is set was refused
> after the facilitator broadcast. Look that transaction up on-chain before you
> do anything else.

## Receipt errors

`client.payment.verify_guarantee` and `verify_guarantee_receipt` raise these `PaymentError`s:

| Error | When it is raised |
| - | - |
| `MalformedReceiptError` | The receipt does not decode: not hex, too short, or an unknown layout. |
| `UnreadableReceiptSignatureError` | The receipt's signature is not a readable ECDSA signature. |
| `ReceiptSignerMismatchError` | The receipt was signed by someone other than core's guarantee signer. Carries `.expected` and `.recovered`. |

## Distinguishing errors

Import the classes you want to handle and give each its own `except` clause, most specific first.

```python theme={null}
from fourmica_sdk import (
    CheckpointRequiredError,  # core wants a checkpoint before issuing more
    ContractError,  # on-chain call failed or unexpected result
    Erc20AllowanceRequiredError,  # a token pull needs an approve() first
    ReceiptSignerMismatchError,  # the receipt was not signed by core's guarantee signer
    RpcError,  # 4Mica core service error (has .status_code, .code, .details)
)

try:
    receipt = await client.payment.issue_guarantee(request)
    guarantee = client.payment.verify_guarantee(receipt)
    await client.settlement.pay(cycle_id).send()
except CheckpointRequiredError as err:
    ...  # the payer must checkpoint err.details.cycle_id before core issues more
except ReceiptSignerMismatchError:
    ...  # bad receipt: core's guarantee signer did not sign this guarantee
except Erc20AllowanceRequiredError:
    await client.settlement.pay(cycle_id).self_funded().approve()
except ContractError:
    ...  # on-chain settlement failed
except RpcError as err:
    print(err.status_code, err.code, err)
```

> [!NOTE]
>
> Every error derives from `FourMicaError`, so you can catch that base class to
> handle any SDK failure in one place.

## Next steps

> [card] [Overview](https://docs.4mica.io/sdks/python/overview.md)
>
> Install the SDK and see the capabilities at a glance.

> [card] [Client operations](https://docs.4mica.io/sdks/python/client-operations.md)
>
> Deposit collateral, sign payments, and settle cleared cycles.


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