try/catch to handle errors:
try {
const customer = await paykit.customers.create({
email: 'user@example.com',
name: 'Jane Doe',
});
console.log(customer.id);
} catch (error) {
console.error(error.message);
}
Error types
Import from@paykit-sdk/core:
import {
ValidationError,
ConfigurationError,
ProviderNotSupportedError,
NotImplementedError,
} from '@paykit-sdk/core';
| Error | When |
|---|---|
ValidationError | Input fails schema validation |
ConfigurationError | Provider misconfigured or missing env vars |
ProviderNotSupportedError | Operation not supported by this provider |
NotImplementedError | Operation planned but not yet built |
Example
try {
const checkout = await paykit.checkouts.create({
customer: 'cus_123',
item_id: 'price_123',
session_type: 'one_time',
quantity: 1,
success_url: 'https://example.com/success',
cancel_url: 'https://example.com/cancel',
});
redirect(checkout.payment_url);
} catch (error) {
if (error instanceof ValidationError) {
return res.status(400).json({ error: error.message });
}
if (error instanceof ProviderNotSupportedError) {
return res.status(501).json({ error: 'Not supported by this provider' });
}
return res.status(500).json({ error: 'Unexpected error' });
}