Skip to navigation

Go SDK errors

The one error value your code needs, the responses the SDK sends, and how to handle them.
View as Markdown

The Go SDK keeps errors out of your way. Problems with your setup are reported when your program starts, either as an error from mitu.New or as a panic from Charge. Everything that happens while a request is being handled, from a missing payment to an outage at Mitu, is answered by the middleware itself, and your handler only runs once a payment has settled.

ErrConfig

var ErrConfig = errors.New("mitu: invalid configuration")

ErrConfig is the error that every configuration problem wraps. You will meet it in two places.

  • From mitu.New.
    If your key or default currency is invalid, New returns an error wrapping ErrConfig.
  • From Charge.
    If a fixed price is invalid, for example because the amount has three decimal places or the Price sets both Amount and AmountFunc, Charge panics with an error wrapping ErrConfig.

Use errors.Is to recognise it.

m, err := mitu.New(ctx, opts)
if errors.Is(err, mitu.ErrConfig) {
log.Fatalf("Mitu is misconfigured: %v", err)
}

Responses the SDK sends for you

The table below shows how the middleware answers each kind of request. In every case except the last, your handler does not run.

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 response, passed on unchanged
Mitu is unreachable, rate limiting or failing503 Service Unavailable, with a Retry-After header
Mitu refuses a call from the SDK, for example because the key was revoked500 Internal Server Error, with the cause written to the log
AmountFunc returns an error or an invalid amount500 Internal Server Error
The payment has settledYour response, with the payment receipt in its headers

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

  • Fail fast at startup.
    Treat an ErrConfig from mitu.New as fatal, and register your routes when the program starts. That way an invalid price causes a panic before you serve any traffic, rather than surprising you later.
  • Keep an eye on 500 responses.
    A 500 from a charged route usually means your key needs attention. Pass a Logger in Options so that the cause is recorded and you can act on it quickly.
  • Never charge twice for one result.
    Once a payment has settled it is final. If your handler fails after the caller has paid, retry the work or refund the payment instead of asking the caller to pay again.