Skip to content

Commit

Permalink
raise inextricable response body error
Browse files Browse the repository at this point in the history
for when the body isn't extractable because it's not one of
known/expected body types. at the moment the raise results in a mere
runtime error, which isn't informative.
  • Loading branch information
yawboakye committed Jun 26, 2024
1 parent b0e9bae commit 2b04381
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/vigiles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ def self.spec=(spec)
@spec = spec
end

sig { params(req: ActionDispatch::Request, res: Rack::Response).returns(T.nilable(Archive::Conversation)) }
sig do
params(
req: ActionDispatch::Request,
res: Rack::Response
)
.returns(T.nilable(Archive::Conversation))
end
def self.maybe_record_conversation(req:, res:)
return unless should_record?(req)

Expand All @@ -47,7 +53,7 @@ def self.maybe_record_conversation(req:, res:)
def self.configure(&blk)
blk.call(spec)

# TODO(yaw, 2024-06-15): ensure that the spec is valid.
# TODO(yaw, 2024-07-15): ensure that the spec is valid.
# ensure that for every content type a recorder is configured. otherwise
# assign the general recorder for unknown content types.
end
Expand Down
13 changes: 12 additions & 1 deletion lib/vigiles/archive/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ class Response < T::Struct
const :payload, Types::Payload
const :status, Integer

class InextricableResponseBodyError < StandardError
sig { returns(String) }
attr_reader :response_body_class

sig { params(response_body_class: String).void }
def initialize(response_body_class:)
@response_body_class = response_body_class
super("failed to extract response body: response_body_class=#{response_body_class}")
end
end

class ResponseBodyTooDeepError < StandardError
sig { returns(Integer) }
attr_reader :max_stack_depth
Expand All @@ -33,7 +44,7 @@ def initialize(stack_depth, max_stack_depth)
case (inner_body = body.instance_variable_get(:@body))
when Rack::BodyProxy then extract_body_from_rack_body_proxy(inner_body, stack_depth + 1)
when Array then inner_body[0] || "null"
else raise
else raise InextricableResponseBodyError.new(inner_body.class.name)
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/vigiles/utilities/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def self.parse_benignly(text)
::JSON.parse(text)
rescue StandardError
return text unless block_given?

yield text
end
end
Expand Down

0 comments on commit 2b04381

Please sign in to comment.