Refund Error Handling — Server Crash
A payment gateway rejection should never take the application down with it.
A refund request Razorpay rejected didn't fail cleanly — it crashed the server process.
I reproduced the crash with a refund I knew Test Mode would reject, then looked at
the actual shape of the thrown error instead of trusting the SDK's documented
format. The refund route's catch block was calling .message.includes()
on it, assuming a .message string the way a standard JavaScript Error
exposes one.
The SDK error didn't populate .message the way the handler assumed.
Calling .includes() on undefined threw a second, unhandled
exception inside the error handler itself — and that's what actually took the
process down. The information the handler needed was available on
error.statusCode.
Rewrote the handler to read error.statusCode and return a sanitized,
controlled error response instead of trusting SDK internals it hadn't verified.
REF-ERR-001 and REF-ERR-002 now assert the server stays
alive and returns a proper error response whenever Razorpay rejects a refund.
The same rejected refund that used to crash the process now returns a controlled response, and the server keeps running.