Skip to content

Conversation

@avokhmin
Copy link
Contributor

Summary

Adds a new ForbiddenError exception class to properly handle HTTP 403 Forbidden responses from the ChargeBee API. This provides better error handling and allows applications to distinguish between forbidden access errors and other API errors.

Changes

Error Handling

  • Added new ForbiddenError class that inherits from Error
  • Enhanced handle_for_error method to detect and handle 403 status codes
  • Provides clear error message: "Access forbidden. You do not have permission to access this resource."

Code Quality Improvements

  • Renamed lib/chargebee/nativeRequest.rblib/chargebee/native_request.rb to follow Ruby naming conventions
  • Refactored handle_for_error to return error objects instead of raising them inline (cleaner separation of concerns)
  • Added YARD documentation for handle_for_error method

Testing

  • Added comprehensive test coverage for 403 error scenarios

Motivation

When API keys lack permissions or access is restricted, the ChargeBee API returns a 403 status code. Previously, this was handled as a generic error, making it difficult for applications to distinguish permission issues from other error types. With ForbiddenError, applications can now:

  • Catch and handle permission errors specifically
  • Provide better user feedback about access issues
  • Implement custom retry or fallback logic for authorization failures

Example Usage

begin
  ChargeBee::Customer.update(...)
rescue ChargeBee::ForbiddenError => e
  # Handle permission errors specifically
rescue ChargeBee::APIError => e
  # Handle other API errors
end

- Add new ForbiddenError class to handle 403 Forbidden responses
- Rename nativeRequest.rb to native_request.rb to follow Ruby naming conventions
- Refactor handle_for_error to return error objects instead of raising them directly
- Add test coverage for 403 error handling with HTML response body
- Add YARD documentation for handle_for_error method
@cb-alish cb-alish merged commit 26559d6 into chargebee:master Nov 10, 2025
3 checks passed
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.

2 participants