Skip to content

feat: add statusCode, code, and description to RazorpayException#349

Open
sahilleth wants to merge 1 commit intorazorpay:masterfrom
sahilleth:feat-razorpay-exception-error-fields
Open

feat: add statusCode, code, and description to RazorpayException#349
sahilleth wants to merge 1 commit intorazorpay:masterfrom
sahilleth:feat-razorpay-exception-error-fields

Conversation

@sahilleth
Copy link

Summary

Adds statusCode, code, and description to RazorpayException to support programmatic error handling.

Problem

RazorpayException only exposes getMessage(), so callers must parse strings to handle different API errors (e.g. 400 vs 404 vs 429). This makes retries, rate limiting, and branching by error type difficult.

Solution

  • Add getStatusCode(), getCode(), getDescription() to RazorpayException
  • Populate these from API error responses in ApiClient.throwException()
  • Use optString for optional code and description (handles OAuth responses where these may be absent)
  • Add serialVersionUID for serialization compatibility
  • Backward compatible: existing constructors and usage remain unchanged

Usage

try {
Payment payment = razorpayClient.payments.fetch(paymentId);
} catch (RazorpayException e) {
if (Integer.valueOf(429).equals(e.getStatusCode())) {
// Retry with backoff
} else if ("BAD_REQUEST_ERROR".equals(e.getCode())) {
// Handle validation error
}
}

Testing
RazorpayExceptionTest: constructors and getters
PaymentClientTest#fetchThrowsExceptionWithStatusCodeAndErrorCode: API error path

Enables programmatic error handling for API errors (retry logic,
rate limiting, branching by error type). Backward compatible.

- Add getStatusCode(), getCode(), getDescription() getters
- Populate from API error responses in ApiClient
- Use optString for optional code/description (OAuth compatibility)
- Add serialVersionUID for safe serialization
- Add RazorpayExceptionTest and error-path test in PaymentClientTest

Made-with: Cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant