Skip to content

Add pattern matching support to Net::HTTPExceptions#243

Open
baweaver wants to merge 2 commits intoruby:masterfrom
baweaver:baweaver/exceptions-pattern-matching
Open

Add pattern matching support to Net::HTTPExceptions#243
baweaver wants to merge 2 commits intoruby:masterfrom
baweaver:baweaver/exceptions-pattern-matching

Conversation

@baweaver
Copy link
Copy Markdown
Contributor

@baweaver baweaver commented Nov 9, 2025

This PR adds deconstruct_keys to the Net::HTTPExceptions module, enabling pattern matching on HTTP exception classes for cleaner error handling.

Usage

begin
  http.request(req)
rescue => e
  case e
  in HTTPRetriableError[response: res] if res.code == '503'
    retry_with_backoff
  in HTTPClientException[message: /not found/]
    handle_not_found
  in HTTPFatalError[response:]
    log_fatal_error(response)
  end
end

Implementation

  • Added PATTERN_MATCHING_KEYS constant to HTTPExceptions module with keys: message, response
  • Implemented deconstruct_keys following Ruby pattern matching best practices
  • Applies to all exception classes: HTTPError, HTTPRetriableError, HTTPClientException, HTTPFatalError
  • Tests wrapped in instance_eval with SyntaxError rescue for Ruby 2.6 compatibility

Loading
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