Skip to content

Commit

Permalink
convert response to an %Error{} when raising
Browse files Browse the repository at this point in the history
  • Loading branch information
scrogson committed Feb 2, 2017
1 parent f5fdea6 commit 8a21564
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/oauth2/request.ex
Expand Up @@ -43,8 +43,17 @@ defmodule OAuth2.Request do
case request(method, client, url, body, headers, opts) do
{:ok, resp} ->
resp
{:error, %{status_code: code, body: body}} ->
raise %Error{reason: "Server responded with status: #{code}; body: #{inspect body}"}
{:error, %{status_code: code, headers: headers, body: body}} ->
raise %Error{reason: """
Server responded with status: #{code}
Headers:
#{Enum.reduce(headers, "", fn {k, v}, acc -> acc <> "#{k}: #{v}\n" end)}
Body:
#{inspect body}
"""}
{:error, error} ->
raise error
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Expand Up @@ -7,7 +7,7 @@ defmodule OAuth2.Mixfile do
[app: :oauth2,
name: "OAuth2",
version: @version,
elixir: "~> 1.2 or ~> 1.3",
elixir: "~> 1.2",
deps: deps(),
package: package(),
description: description(),
Expand Down
2 changes: 1 addition & 1 deletion test/oauth2/client_test.exs
Expand Up @@ -284,7 +284,7 @@ defmodule OAuth2.ClientTest do
json(conn, 400, %{error: "error"})
end

assert_raise OAuth2.Error, ~r/Server responded with status: 400; body:/, fn ->
assert_raise OAuth2.Error, ~r/Server responded with status: 400/, fn ->
Client.get!(client, "/api/error")
end
end
Expand Down

0 comments on commit 8a21564

Please sign in to comment.