Skip to navigation

Python SDK errors

What the SDK answers for you, which exceptions reach your code, and how to handle them.
View as Markdown

Most things that can go wrong with a payment never reach your code. If a caller has not paid, has paid for the wrong route or has had a payment refused, the SDK replies to the caller directly and your view does not run. The route stays closed until a payment has settled.

Your code only sees an exception in two situations. The first is a problem with your setup, such as an invalid key or price. The second is Mitu rejecting a call from the SDK itself, which usually means your key needs attention. This page covers both.

Responses the SDK sends for you

The table below shows how the SDK answers each kind of request. In every case except the last two, your view does not run. When the payment has settled, your view runs with the payment available to it. When the price for a request is None, it runs without one.

SituationWhat the caller receives
The request carries no payment402 Payment Required, with MPP and x402 payment options
The payment was made for a different route, method, amount or currencyA new 402 for this route and price
Mitu refuses the paymentMitu’s refusal, passed on unchanged
Mitu is unreachable, rate limiting or failing503 Service Unavailable, with a Retry-After header
The payment has settledYour response, with the payment receipt in its headers
The price for this request is NoneYour response

An outage at Mitu never causes a route to be served unpaid. The caller is told to try again later instead.

Exception classes

Every exception the package raises is a subclass of MituError. Exception messages never contain your key or a payment credential, so they are safe to log.

class MituError(Exception):
code = "MITU_ERROR"
class MituConfigError(MituError):
code = "MITU_CONFIG"
class MituApiError(MituError):
code = "MITU_API"
status: int
ExceptionWhen it is raised
MituConfigErrorYour API key, a currency or a price is invalid. Fixed prices are checked when you create the client and when you apply @mitu.charge.
MituApiErrorMitu rejected a call from the SDK in a way that retrying will not fix. The most common cause is a key that has been revoked. The status attribute holds the HTTP status Mitu returned.

Rate limiting and server errors at Mitu are temporary, so the SDK does not raise them. It answers the caller with the 503 response described above instead.

Handling exceptions

A MituConfigError means something is wrong with how the SDK has been set up. Treat it as a deployment problem and let your application fail to start, so that the mistake is caught before any traffic arrives.

A MituApiError that reaches your code usually means the key has been revoked or is no longer valid. Handle it in your framework’s error handling, log it, and alert whoever looks after your Mitu account. The example below shows one way to do this in FastAPI.

from fastapi.responses import JSONResponse
from mitulabs_payments import MituApiError
@app.exception_handler(MituApiError)
async def mitu_rejected(request: Request, error: MituApiError):
logger.error("Mitu rejected the SDK (%s)", error.status)
return JSONResponse({"error": "payment_unavailable"}, status_code=500)

When your view fails after payment

Once a payment has settled it is final. If your view fails after the caller has paid, do not ask the caller to pay again. Retry the work, or refund the payment, so that the caller is never charged twice for one result.