Skip to content

feat(errors): add RazorpayError as common base exception class - #349

Open
Sreejith088 wants to merge 1 commit into
razorpay:masterfrom
Sreejith088:fix/base-exception-class
Open

Sreejith088 wants to merge 1 commit into
razorpay:masterfrom
Sreejith088:fix/base-exception-class

Conversation

@Sreejith088

Copy link
Copy Markdown

Summary

Closes #52

All SDK exceptions (BadRequestError, GatewayError, ServerError, SignatureVerificationError) previously inherited directly from Python's built-in Exception. This meant callers had to catch each error type separately, which is both verbose and fragile — any new exception type added to the SDK would silently escape a catch-all handler.

This PR introduces RazorpayError(Exception) as a shared base class. All four existing exceptions now inherit from it, so users can write:

try:
    client.payment.capture(payment_id, amount)
except razorpay.RazorpayError as e:
    # handles BadRequestError, GatewayError, ServerError, etc.
    handle_error(e)

while still being able to catch specific subtypes when needed.

RazorpayError is also exported from the top-level package so it is available as razorpay.RazorpayError without an extra import.

Changes

  • razorpay/errors.py: added RazorpayError base class; updated all four exceptions to inherit from it
  • razorpay/__init__.py: exported all five exception classes
  • tests/test_client_error.py: added 4 tests — 3 asserting the inheritance relationships and 1 confirming a live BadRequestError is catchable as RazorpayError

Test plan

  • test_bad_request_error_is_razorpay_error
  • test_gateway_error_is_razorpay_error
  • test_server_error_is_razorpay_error
  • test_catch_as_razorpay_error
  • All existing error tests continue to pass (8 passed)

Closes razorpay#52

All SDK exceptions (BadRequestError, GatewayError, ServerError,
SignatureVerificationError) previously inherited directly from Python's
built-in Exception, making it impossible for callers to catch all
Razorpay-specific errors with a single except clause.

Introduces RazorpayError(Exception) as a shared base class and updates
all four exception classes to inherit from it. Also exports RazorpayError
from the top-level package so users can import it alongside Client.

Adds four tests: three asserting the inheritance relationship and one
confirming that a live BadRequestError is catchable as RazorpayError.
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.

Exceptions does not inherit from a base exception

1 participant