Skip to content

Commit

Permalink
Add additional information to the InvalidRequestError message.
Browse files Browse the repository at this point in the history
When using additional match criteria via the `:match_requests_on` option this `InvalidRequestError` message will now report the components that were matched against. The aim is to improve the developer experience. 🧑‍💻
  • Loading branch information
sh41 committed Aug 24, 2022
1 parent 69aaa49 commit 333c53b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/exvcr/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule ExVCR.Handler do
case { response, stub_mode?(recorder_options) } do
{ nil, true } ->
raise ExVCR.InvalidRequestError,
message: "response for [URL:#{params[:url]}, METHOD:#{params[:method]}] was not found"
message: "response for [#{invalid_request_details(recorder_options, params)}] was not found"
{ nil, false } ->
nil
{ response, _ } ->
Expand All @@ -45,6 +45,22 @@ defmodule ExVCR.Handler do
end
end

defp invalid_request_details(recorder_options, params) do
available_match_types = [:headers, :query, :request_body]
match_requests_on = Keyword.get(recorder_options, :match_requests_on, [])

extra_details =
for type <- available_match_types, type in match_requests_on do
String.upcase("#{type}") <> ":#{inspect(params[type])}"
end

([
"URL:#{params[:url]}",
"METHOD:#{params[:method]}"
] ++ extra_details)
|> Enum.join(", ")
end

defp stub_mode?(options) do
options[:custom] == true || options[:stub] != nil
end
Expand Down

0 comments on commit 333c53b

Please sign in to comment.