Skip to navigation

TypeScript SDK errors

What the SDK answers for you, which errors 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 handler does not run. The route stays closed until a payment has settled.

Your code only sees an error 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 handler does not run. When the payment has settled, your handler runs with the payment available to it. When the price for a request is null, 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 nullYour response

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

Error classes

Every error the package throws is an instance of MituError. Error messages never contain your key, a payment credential or any request data, so they are safe to log.

class MituError extends Error {
readonly code: 'MITU_CONFIG' | 'MITU_API'
}
class MituConfigError extends MituError {}
class MituApiError extends MituError {
readonly status: number
readonly retryable: boolean
}
ClassCodeWhen it is thrown
MituConfigErrorMITU_CONFIGYour API key, a currency or a price is invalid. Fixed prices are checked at startup by createMitu and charge. Prices returned by a function are checked on each request.
MituApiErrorMITU_APIMitu 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.

MituApiError has two extra properties. The status property holds the HTTP status Mitu returned, or 0 if no response arrived at all. The retryable property is true for network failures, rate limiting and server errors. The SDK already turns those temporary failures into the 503 response described above, so the MituApiError instances that reach your code are the ones a retry will not fix.

Handling errors

A MituConfigError means something is wrong with how the SDK has been set up. Treat it as a deployment problem and let your server 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. Catch it in your framework’s error handler, log it, and alert whoever looks after your Mitu account. The example below shows one way to do this in Express.

import { MituApiError } from '@mitulabs/payments'
app.use((err, req, res, next) => {
if (err instanceof MituApiError) {
logger.error('Mitu rejected the SDK', { status: err.status })
return res.status(500).json({ error: 'payment_unavailable' })
}
next(err)
})

When your handler fails after payment

Once a payment has settled it is final. If your handler 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.