Skip to content

Commit

Permalink
Add additional information to the InvalidRequestError message. (#188)
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 Sep 10, 2022
1 parent 77348e2 commit c8e22ae
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/exvcr/handler.ex
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 c8e22ae

Please sign in to comment.