Go SDK errors
The one error value your code needs, the responses the SDK sends, and how to handle them.
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
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,Newreturns an error wrappingErrConfig. - From
Charge.
If a fixed price is invalid, for example because the amount has three decimal places or thePricesets bothAmountandAmountFunc,Chargepanics with an error wrappingErrConfig.
Use errors.Is to recognise it.
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.
An outage at Mitu never causes a route to be served unpaid. The caller is told to try again later instead.
Recommended handling
- Fail fast at startup.
Treat anErrConfigfrommitu.Newas 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 aLoggerinOptionsso 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.