Rulesnorthwind-charge-error-unchecked

northwind-charge-error-unchecked

warning

northwind/reliability/charge-error-unchecked

Docs page ↗

The result of charges.create is used immediately — no try/catch around the call and no check of charge.status — so a decline flows straight into fulfilment.

19

codebases affected, of 58

33%

of everything we scanned

no docs fix scored yet

0

docs commits aimed at this

Where it’s failing

Charge result is used without catching errors or checking charge.status.

what we find

what we change it to

const charge = await northwind.charges.create({
amount: order.totalCents, currency: 'usd', customer,
})
await orders.markPaid(order.id, charge.id)
await shipping.enqueue(order.id) // ships on a declined card
let charge
try {
charge = await northwind.charges.create({
amount: order.totalCents, currency: 'usd', customer,
})
} catch (err) {
return res.status(502).json({ error: err.message })
}
if (charge.status !== 'succeeded') {
return res.status(402).json({ error: charge.failure_code })
}
await orders.markPaid(order.id, charge.id)
await shipping.enqueue(order.id)

Wrap charges.create in try/catch to handle card errors, then gate fulfilment on charge.status === "succeeded" and surface charge.failure_code to the buyer.

Metadata

Category
reliability
Type
suggestion
Severity
warning
Recommended
yes
Languages
javascriptpython

Live telemetry

19 / 58 scans

Fired in 33% of scanned codebases.

After every docs commit

No docs commits target this rule yet. When one merges, the next weeks of scans re-score it here.

A fix for your docs page

Wherever this rule fires, a page on your docs site taught it. api-doctor writes the edit and opens it as a pull request on your docs repo — you review and merge.

No fix yet

Draft a docs fix for this rule

api-doctor writes the docs change from the fire pattern — you review it