43. Review this API handler. What production risks do you see?
MediumDebugging & production
Technical
async function payOrder(order, card) {
logger.info('paying', { order, card });
db.orders.update(order.id, { status: 'paid' });
try {
await paymentApi.charge({ amount: order.total, card });
} catch (e) {
// ignore
}
return { ok: true };
}Show model answer
Risks: (1) no timeout on the payment call, so a hung provider ties up workers until the pool is exhausted; (2) no retry for transient errors, or, if someone adds a naive retry, a duplicate charge, because there is no idempotency key; (3) the order is marked paid before checking the response; (4) errors are swallowed, so failures are invisible; (5) the full card object is logged, which leaks sensitive data.
Fix: set a timeout, retry only transient failures with bounded exponential backoff and jitter, send an idempotency key derived from the order id, update state only on confirmed success, return a clear error, emit a metric, and never log payment details.