Skip to content

Commit 07f1cf8

Browse files
TheBlackArroVVixti
andauthored
feat: introduce better error for wrong mime type (#805)
Now one can do: begin payload = HTTP.get("https://example.com").parse rescue HTTP::ParseError payload = {} end Co-authored-by: Alexey Zapparov <alexey@zapparov.com>
1 parent a8e7e74 commit 07f1cf8

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

lib/http/errors.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ def initialize(response)
3232
end
3333
end
3434

35+
# Raised when `Response#parse` fails due to any underlying reason (unexpected
36+
# MIME type, or decoder fails). See `Exception#cause` for the original exception.
37+
class ParseError < ResponseError; end
38+
39+
# Requested MimeType adapter not found.
40+
class UnsupportedMimeTypeError < Error; end
41+
3542
# Generic Timeout error
3643
class TimeoutError < Error; end
3744

lib/http/mime_type.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "http/errors"
4+
35
module HTTP
46
# MIME type encode/decode adapters
57
module MimeType
@@ -35,7 +37,7 @@ def register_adapter(type, adapter)
3537
# @raise [Error] if no adapter found
3638
# @return [Class]
3739
def [](type)
38-
adapters[normalize type] || raise(Error, "Unknown MIME type: #{type}")
40+
adapters[normalize type] || raise(UnsupportedMimeTypeError, "Unknown MIME type: #{type}")
3941
end
4042

4143
# Register a shortcut for MIME type

lib/http/response.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "forwardable"
44

5+
require "http/errors"
56
require "http/headers"
67
require "http/content_type"
78
require "http/mime_type"
@@ -155,6 +156,8 @@ def chunked?
155156
# @return [Object]
156157
def parse(type = nil)
157158
MimeType[type || mime_type].decode to_s
159+
rescue => e
160+
raise ParseError, e.message
158161
end
159162

160163
# Inspect a response

spec/lib/http/response_spec.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@
109109
context "with unknown content type" do
110110
let(:content_type) { "application/deadbeef" }
111111

112-
it "raises HTTP::Error" do
113-
expect { response.parse }.to raise_error HTTP::Error
112+
it "raises HTTP::ParseError" do
113+
expect { response.parse }.to raise_error HTTP::ParseError
114114
end
115115
end
116116

@@ -125,6 +125,15 @@
125125
expect(response.parse(:json)).to eq "foo" => "bar"
126126
end
127127
end
128+
129+
context "when underlying parser fails" do
130+
let(:content_type) { "application/deadbeef" }
131+
let(:body) { "" }
132+
133+
it "raises HTTP::ParseError" do
134+
expect { response.parse }.to raise_error HTTP::ParseError
135+
end
136+
end
128137
end
129138

130139
describe "#flush" do

0 commit comments

Comments
 (0)