Skip to main content

Error Handling

foxses-pay throws typed errors so you can handle each case precisely.

Error Classes

ClassCodeWhen it happens
AuthenticationErrorAUTHENTICATION_ERRORWrong API key, expired token, invalid credentials
ValidationErrorVALIDATION_ERRORMissing required params, invalid amount, wrong format
NetworkErrorNETWORK_ERRORHTTP failure, timeout, DNS error
ProviderErrorPROVIDER_ERRORProvider returned an error response
PaymentErrorPAYMENT_ERRORBase class — catch all payment errors

All extend PaymentError which extends Error.

Usage

import {
AuthenticationError,
ValidationError,
NetworkError,
ProviderError,
PaymentError,
} from "foxses-pay";

try {
const payment = await gateway.createPayment("bkash", params);
} catch (err) {
if (err instanceof AuthenticationError) {
// Credentials wrong — check your API keys
console.error("Auth failed:", err.message);
console.error("Provider:", err.provider); // "bkash"

} else if (err instanceof ValidationError) {
// Something missing or invalid in your request
console.error("Validation:", err.message);

} else if (err instanceof NetworkError) {
// Could not reach the provider — retry or alert
console.error("Network issue:", err.message);

} else if (err instanceof ProviderError) {
// Provider responded with an error
console.error("Provider error:", err.message);

} else if (err instanceof PaymentError) {
// Any payment-related error
console.error("Payment error:", err.message, err.code);
}
}

Error Properties

Every error exposes:

err.message // human-readable description
err.code // "AUTHENTICATION_ERROR" | "VALIDATION_ERROR" | ...
err.provider // "bkash" | "nagad" | "sslcommerz" | undefined
err.name // class name: "AuthenticationError" etc.
err.stack // stack trace

Common Errors by Provider

bKash

ErrorCauseFix
AuthenticationErrorWrong appKey/appSecret/username/passwordCheck merchant dashboard credentials
ProviderError: statusCode 2001Invalid payment amountAmount must be > 0
ProviderError: statusCode 2062Duplicate orderIdUse a unique orderId per payment
NetworkErrorCannot reach bKash serversCheck internet / sandbox URL

Nagad

ErrorCauseFix
ProviderErrorRSA encryption failedCheck private key / nagadPublicKey format
ProviderErrorDuplicate orderIdUse a unique orderId per payment
NetworkErrorCannot reach Nagad serversSandbox may require specific IP whitelist

SSLCommerz

ErrorCauseFix
ProviderError: FAILEDWrong storeId/storePasswordCheck store credentials
ProviderError: amount mismatchResponse amount differs from expectedPossible tampered response — do not fulfill order
ProviderError: INVALID_TRANSACTIONval_id does not exist or expiredDo not fulfill order

Retry Strategy

Retry only NetworkError — never retry ProviderError or ValidationError.

async function createPaymentWithRetry(
provider: string,
params: CreatePaymentParams,
retries = 2
) {
for (let attempt = 0; attempt <= retries; attempt++) {
try {
return await gateway.createPayment(provider as any, params);
} catch (err) {
if (err instanceof NetworkError && attempt < retries) {
await new Promise(r => setTimeout(r, 1000 * (attempt + 1))); // backoff
continue;
}
throw err;
}
}
}