Skip to content

Commit 0d62e17

Browse files
committed
Fix for non-json encoded payloads
This will make two attempts to decode the payload. If it cannot decode the first time it will simply encode the payload then re-attempt to decode. While this feels like just additional overhead (why not just reply with the original payload?) this will hopefully keep the API on the path of using JSON responses an anticipation that the Chromium team fixes this bug upstream. This way we don't have to introduce a bunch of forking code to detect different payload types.
1 parent d9b55bb commit 0d62e17

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/http.ex

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ defmodule ChromeRemoteInterface.HTTP do
2828
defp handle_response({:ok, status_code, _response_headers, client_ref}) do
2929
with true <- status_code >= 200 && status_code < 300,
3030
{:ok, body} <- :hackney.body(client_ref),
31-
{:ok, json} <- format_body(body) |> Poison.decode() do
31+
{:ok, formatted_body} <- format_body(body),
32+
{:ok, json} <- decode(formatted_body) do
3233
{:ok, json}
3334
else
3435
error -> error
@@ -43,6 +44,13 @@ defmodule ChromeRemoteInterface.HTTP do
4344
{:error, reason}
4445
end
4546

46-
defp format_body(""), do: "{}"
47-
defp format_body(body), do: body
47+
defp format_body(""), do: format_body("{}")
48+
defp format_body(body), do: {:ok, body}
49+
50+
defp decode(body) do
51+
case Poison.decode(body) do
52+
{:ok, json} -> {:ok, json}
53+
{:error, _reason} -> {:ok, body}
54+
end
55+
end
4856
end

0 commit comments

Comments
 (0)