Skip to content

Conversation

@baweaver
Copy link
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

Implements deconstruct_keys for HTTP exception classes to enable
pattern matching on error handling.

Example:
  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
    end
  end
# http.request(req)
# rescue => e
# case e
# in HTTPRetriableError[response: { code: '503' }]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming that #240 is merged this is possible, otherwise nested deconstruction is not quite possible hence the deviation in the tests.

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