Skip to content

Commit

Permalink
Merge branch 'main' into acceptable
Browse files Browse the repository at this point in the history
# Conflicts:
#	lib/http/feature.rb
  • Loading branch information
c960657 committed Nov 12, 2024
2 parents ce6a0d7 + db6863a commit 21aecc1
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/http/chainable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def nodelay
# * instrumentation
# * logging
# * normalize_uri
# * raise_error
# @param features
def use(*features)
branch default_options.with_features(features)
Expand Down
11 changes: 11 additions & 0 deletions lib/http/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ class ResponseError < Error; end
# Requested to do something when we're in the wrong state
class StateError < ResponseError; end

# When status code indicates an error
class StatusError < ResponseError
attr_reader :response

def initialize(response)
@response = response

super("Unexpected status code #{response.code}")
end
end

# Generic Timeout error
class TimeoutError < Error; end

Expand Down
5 changes: 3 additions & 2 deletions lib/http/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ def on_error(_request, _error); end
end
end

require "http/features/acceptable"
require "http/features/auto_inflate"
require "http/features/auto_deflate"
require "http/features/acceptable"
require "http/features/logging"
require "http/features/instrumentation"
require "http/features/logging"
require "http/features/normalize_uri"
require "http/features/raise_error"
22 changes: 22 additions & 0 deletions lib/http/features/raise_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module HTTP
module Features
class RaiseError < Feature
def initialize(ignore: [])
super()

@ignore = ignore
end

def wrap_response(response)
return response if response.code < 400
return response if @ignore.include?(response.code)

raise HTTP::StatusError, response
end

HTTP::Options.register_feature(:raise_error, self)
end
end
end
62 changes: 62 additions & 0 deletions spec/lib/http/features/raise_error_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

RSpec.describe HTTP::Features::RaiseError do
subject(:feature) { described_class.new(ignore: ignore) }

let(:connection) { double }
let(:status) { 200 }
let(:ignore) { [] }

describe "#wrap_response" do
subject(:result) { feature.wrap_response(response) }

let(:response) do
HTTP::Response.new(
version: "1.1",
status: status,
headers: {},
connection: connection,
request: HTTP::Request.new(verb: :get, uri: "https://example.com")
)
end

context "when status is 200" do
it "returns original request" do
expect(result).to be response
end
end

context "when status is 399" do
let(:status) { 399 }

it "returns original request" do
expect(result).to be response
end
end

context "when status is 400" do
let(:status) { 400 }

it "raises" do
expect { result }.to raise_error(HTTP::StatusError, "Unexpected status code 400")
end
end

context "when status is 599" do
let(:status) { 599 }

it "raises" do
expect { result }.to raise_error(HTTP::StatusError, "Unexpected status code 599")
end
end

context "when error status is ignored" do
let(:status) { 500 }
let(:ignore) { [500] }

it "returns original request" do
expect(result).to be response
end
end
end
end

0 comments on commit 21aecc1

Please sign in to comment.