Use Net::HTTPHeaderSyntaxError instead of ArgumentError#72
Open
kevindew wants to merge 1 commit intoruby:masterfrom
Open
Use Net::HTTPHeaderSyntaxError instead of ArgumentError#72kevindew wants to merge 1 commit intoruby:masterfrom
kevindew wants to merge 1 commit intoruby:masterfrom
Conversation
This change proposes changing the exception class used when invalid
syntax is used in a HTTP Header from ArgumentError to
Net::HTTPHeaderSyntaxError.
The reason I am proposing this change is to make it easier for
applications and libraries to catch errors that result as part of HTTP
response.
For example, if you're calling: `Net::HTTP.get("www.example.com", "/")`
it is confusing to receive an ArgumentError exception as this isn't a
problem with the arguments provided to `Net::HTTP#get` and instead is a
lower-level parsing concern of the work done by `Net::HTTP#get`.
This exception isn't caught by common libraries that wrap Net::HTTP, for
example Faraday: https://github.com/lostisland/faraday-net_http/blob/616bd1c35c058da01f91af8c4f504141b4f2b427/lib/faraday/adapter/net_http.rb#L15-L31
and is a somewhat delicate error to catch when making a request as a
developer likely wants to distinguish between a genuine ArgumentError to
usage of `Net::HTTP` and what is an error in the syntax in the HTTP
request/response.
There was existing precedence for the use of this exception in
Net::HTTPHeader, so this seemed like a change that could made and make
the class behave consistently.
An example of usage is below.
Before this change:
```
irb(main):001:0> require 'webmock'; include WebMock::API; WebMock.enable!; stub_request(:any, "www.example.com").to_return
(headers: { "Invalid" => "Item with \r character" })
irb(main):002:0> Net::HTTP.get("www.example.com", "/")
/Users/kevin.dew/dev/net-http/lib/net/http/header.rb:85:in `set_field': header field value cannot include CR/LF (ArgumentError)
```
After this change:
```
irb(main):001:0> require 'webmock'; include WebMock::API; WebMock.enable!; stub_request(:any, "www.example.com").to_return
(headers: { "Invalid" => "Item with \r character" })
irb(main):002:0> Net::HTTP.get("www.example.com", "/")
/Users/kevin.dew/dev/net-http/lib/net/http/header.rb:87:in `set_field': header field value cannot include CR/LF (Net::HTTPHeaderSyntaxError) => "Item with \r character" })
```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change proposes changing the exception class used when invalid syntax is used in a HTTP Header from ArgumentError to Net::HTTPHeaderSyntaxError.
The reason I am proposing this change is to make it easier for applications and libraries to catch errors that result as part of HTTP response.
For example, if you're calling:
Net::HTTP.get("www.example.com", "/")it is confusing to receive an ArgumentError exception as this isn't a problem with the arguments provided toNet::HTTP#getand instead is a lower-level parsing concern of the work done byNet::HTTP#get.This exception isn't caught by common libraries that wrap Net::HTTP, for example Faraday: https://github.com/lostisland/faraday-net_http/blob/616bd1c35c058da01f91af8c4f504141b4f2b427/lib/faraday/adapter/net_http.rb#L15-L31 and is a somewhat delicate error to catch when making a request as a developer likely wants to distinguish between a genuine ArgumentError to usage of
Net::HTTPand what is an error in the syntax in the HTTP request/response.There was existing precedence for the use of this exception in Net::HTTPHeader, so this seemed like a change that could made and make the class behave consistently.
An example of usage is below.
Before this change:
After this change: