httpoison icon indicating copy to clipboard operation
httpoison copied to clipboard

Handle proxy error

Open MeterSoft opened this issue 6 years ago • 4 comments

How i can handle proxy error like proxy error: "HTTP/1.1 503 Service Unavailable\r\nServer: squid/3.3.8\r\nMime-Version: 1.0\r\nDate: Mon, 05 Mar 2018 17:01:39 GMT\r\nContent-Type: text/html\r\nContent-Length: 3280\r\nX-Squid-Error: ERR_DNS_FAIL 0\r\nVary: Accept-Language\r\nContent-Langu

i have this code

case HTTPoison.get(url, headers, request_options) do
      {:ok, response} ->
        IO.inspect(response)
      {:error, %HTTPoison.Error{reason: reason}} ->
        IO.inspect(reason)
      {:error, error} ->
        IO.inspect(error)
      _ ->
        IO.inspect("Another error")
    end

MeterSoft avatar Mar 26 '18 10:03 MeterSoft

Has anyone got a solution to this? I can't seem to catch this error.

intangere avatar Aug 22 '18 18:08 intangere

@intangere Make sure you are not using async requests, use HTTPoison.get and not HTTPoison.get! (no exclamation mark) because that's going to not return right away. Then if you can not get pattern matching to work like the above, I don't know. {:error, error} will match a proxy error but if you want to be really specific with it you could do something like this:

# pretty much same code as above but with an additional specific pattern match:
      {:error, %HTTPoison.Error{reason: :proxy_error}} ->
        IO.puts("There was a proxy error")

You could do something else instead of IO.puts (I'm sure you already know this).

squarism avatar Sep 11 '18 19:09 squarism

@squarism still not find solution. Can not catch proxy error anyway also try this but it anyway put error into console ` task = Task.async(fn -> HTTPoison.get(url, @headers, request_options) end)

        res = try do
          Task.await(task, Proxy.response_time)
        catch
          :proxy_error, _ ->
            {:error, :timeout}
          :exit, _ -> 
            Task.shutdown(task, :brutal_kill)
            {:error, :timeout}
        end

`

MeterSoft avatar Sep 14 '18 04:09 MeterSoft