Skip to content

Commit

Permalink
Removing the Try catch from around HTTPosion as they don't raise an
Browse files Browse the repository at this point in the history
exception anymore. Reference:
edgurgel/httpoison#27
  • Loading branch information
moski committed Aug 14, 2017
1 parent b8d4b84 commit 99a72bd
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions lib/expander/helpers/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,32 @@ defmodule Expander.Helpers.Http do

@spec expand(String.t) :: String.t
def expand(short) do
try do
case HTTPoison.head(short, ["User-Agent": Expander.Helpers.HttpAgent.agent(short)], []) do
case HTTPoison.head(short, ["User-Agent": Expander.Helpers.HttpAgent.agent(short)], []) do
#
# Some websites like tiny.cc return with HTTP status 303 [SEE OTHER] https://httpstatuses.com/303
#
# if the header is:
# 301 Moved Permanently
# 302 Found
# 303 See Other
#
# then deal with the redirection.
#
{:ok, %HTTPoison.Response{headers: headers, status_code: redirect}} when 301 <= redirect and redirect <= 303 ->
headers |> get_header("location") |> expand

#
# Some websites like tiny.cc return with HTTP status 303 [SEE OTHER] https://httpstatuses.com/303
#
# if the header is:
# 301 Moved Permanently
# 302 Found
# 303 See Other
#
# then deal with the redirection.
#
{:ok, %HTTPoison.Response{headers: headers, status_code: redirect}} when 301 <= redirect and redirect <= 303 ->
headers |> get_header("location") |> expand
#
# Some services disable the HEAD request and return 405 Method Not Allowed
#
{:ok, %HTTPoison.Response{headers: _headers, status_code: 405}} -> {:ok, short}
{:ok, %HTTPoison.Response{headers: _headers, status_code: success}} when 200 <= success and success < 300 -> {:ok, short}

#
# Some services disable the HEAD request and return 405 Method Not Allowed
#
{:ok, %HTTPoison.Response{headers: _headers, status_code: 405}} -> {:ok, short}
{:ok, %HTTPoison.Response{headers: _headers, status_code: success}} when 200 <= success and success < 300 -> {:ok, short}
#
# Default fallback
#
{:ok, %HTTPoison.Response{status_code: redirect}} -> {:error, "#{short} returned with states_code: #{redirect}"}

#
# Default fallback
#
{:ok, %HTTPoison.Response{status_code: redirect}} -> {:error, "#{short} returned with states_code: #{redirect}"}

{:error, %HTTPoison.Error{reason: reason}} -> {:error, "#{short} returned with error: #{reason}"}
end
rescue
x ->
IO.inspect x
{:error, "#{short} raised an unexpected error"}
{:error, %HTTPoison.Error{reason: reason}} -> {:error, "#{short} returned with error: #{reason}"}
end
end
end

0 comments on commit 99a72bd

Please sign in to comment.