Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update OAuth2.Client.get_token! errors #98

Merged
merged 3 commits into from
Oct 6, 2017

Conversation

chrislaskey
Copy link
Contributor

Updates the OAuth2.Client.get_token! function to handle error OAuth2.Response structs. I sent some incomplete data to Facebook Graph and saw this error bubble up:

[error] %ArgumentError{message: "raise/1 expects a module name, string or exception as the first argument, got: %OAuth2.Response{body: %{\"error\" => %{\"code\" => 101, \"fbtrace_id\" => \"Eelth+pfiU9\", \"message\" => \"Missing client_id parameter.\", \"type\" => \"OAuthException\"}}, headers: [{\"www-authenticate\", \"OAuth \\\"Facebook Platform\\\" \\\"invalid_client\\\" \\\"Missing client_id parameter.\\\"\"}, {\"access-control-allow-origin\", \"*\"}, {\"pragma\", \"no-cache\"}, {\"cache-control\", \"no-store\"}, {\"x-fb-rev\", \"3349395\"}, {\"content-type\", \"application/json\"}, {\"x-fb-trace-id\", \"Eelth+pfiU9\"}, {\"facebook-api-version\", \"v2.8\"}, {\"expires\", \"Sat, 01 Jan 2000 00:00:00 GMT\"}, {\"x-fb-debug\", \"9GWPCrYAIzlrSnt0iLqP/3E4hdmOJpP7QXxBOXTG2Ew7CloqVwka+HXAjyCTewfTaH0F/sUVkDLMcm7+6jOEEQ==\"}, {\"date\", \"Thu, 05 Oct 2017 15:36:54 GMT\"}, {\"connection\", \"keep-alive\"}, {\"content-length\", \"114\"}], status_code: 400}"}

Using OAuth2.Request.request! as a reference, I added a similar error handling case to OAuth2.Client.get_token!.

Now a proper error is raised with pretty printed debugging information:

[error] #PID<0.1063.0> running ExampleOAuth2Web.Endpoint terminated
Server: oauth2.example.dev:80 (http)
Request: POST /
** (exit) an exception was raised:
    ** (OAuth2.Error) Server responded with status: 400

Headers:

www-authenticate: OAuth "Facebook Platform" "invalid_client" "Missing client_id parameter."
access-control-allow-origin: *
pragma: no-cache
cache-control: no-store
x-fb-rev: 3349509
content-type: application/json
x-fb-trace-id: CIc8eDTN/BC
facebook-api-version: v2.8
expires: Sat, 01 Jan 2000 00:00:00 GMT
x-fb-debug: wJzExwqDXtAkHKtBNo4xKE21zBO730qctNXaP9A7bqenLRuTkGq109qvx8iOy063OiE3uVunf/Mt1trqvhuwVg==
date: Thu, 05 Oct 2017 15:40:21 GMT
connection: keep-alive
content-length: 115

Body:

%{"error" => %{"code" => 101, "fbtrace_id" => "CIc8eDTN/BC", "message" => "Missing client_id parameter.", "type" => "OAuthException"}}

        (oauth2) lib/oauth2/client.ex:249: OAuth2.Client.get_token!/4

Updates the `OAuth2.Client.get_token!` function to handle error OAuth2.Response structs. I sent some incomplete data to Facebook Graph and saw this error bubble up:

```
[error] %ArgumentError{message: "raise/1 expects a module name, string or exception as the first argument, got: %OAuth2.Response{body: %{\"error\" => %{\"code\" => 101, \"fbtrace_id\" => \"Eelth+pfiU9\", \"message\" => \"Missing client_id parameter.\", \"type\" => \"OAuthException\"}}, headers: [{\"www-authenticate\", \"OAuth \\\"Facebook Platform\\\" \\\"invalid_client\\\" \\\"Missing client_id parameter.\\\"\"}, {\"access-control-allow-origin\", \"*\"}, {\"pragma\", \"no-cache\"}, {\"cache-control\", \"no-store\"}, {\"x-fb-rev\", \"3349395\"}, {\"content-type\", \"application/json\"}, {\"x-fb-trace-id\", \"Eelth+pfiU9\"}, {\"facebook-api-version\", \"v2.8\"}, {\"expires\", \"Sat, 01 Jan 2000 00:00:00 GMT\"}, {\"x-fb-debug\", \"9GWPCrYAIzlrSnt0iLqP/3E4hdmOJpP7QXxBOXTG2Ew7CloqVwka+HXAjyCTewfTaH0F/sUVkDLMcm7+6jOEEQ==\"}, {\"date\", \"Thu, 05 Oct 2017 15:36:54 GMT\"}, {\"connection\", \"keep-alive\"}, {\"content-length\", \"114\"}], status_code: 400}"}
```

Using `OAuth2.Request.request!` as a model, I added a similar error handling case to `OAuth2.Client.get_token!`. Now the error raised looks like:

```
[error] #PID<0.1063.0> running ExampleOAuth2Web.Endpoint terminated
Server: oauth2.example.dev:80 (http)
Request: POST /
** (exit) an exception was raised:
    ** (OAuth2.Error) Server responded with status: 400

Headers:

www-authenticate: OAuth "Facebook Platform" "invalid_client" "Missing client_id parameter."
access-control-allow-origin: *
pragma: no-cache
cache-control: no-store
x-fb-rev: 3349509
content-type: application/json
x-fb-trace-id: CIc8eDTN/BC
facebook-api-version: v2.8
expires: Sat, 01 Jan 2000 00:00:00 GMT
x-fb-debug: wJzExwqDXtAkHKtBNo4xKE21zBO730qctNXaP9A7bqenLRuTkGq109qvx8iOy063OiE3uVunf/Mt1trqvhuwVg==
date: Thu, 05 Oct 2017 15:40:21 GMT
connection: keep-alive
content-length: 115

Body:

%{"error" => %{"code" => 101, "fbtrace_id" => "CIc8eDTN/BC", "message" => "Missing client_id parameter.", "type" => "OAuthException"}}

        (oauth2) lib/oauth2/client.ex:249: OAuth2.Client.get_token!/4
```
@coveralls
Copy link

coveralls commented Oct 5, 2017

Coverage Status

Coverage decreased (-2.4%) to 92.118% when pulling a4bc623 on chrislaskey:get-token-error into 246897a on scrogson:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage decreased (-2.4%) to 92.118% when pulling a4bc623 on chrislaskey:get-token-error into 246897a on scrogson:master.

@coveralls
Copy link

coveralls commented Oct 5, 2017

Coverage Status

Coverage decreased (-0.4%) to 94.089% when pulling e46dd8d on chrislaskey:get-token-error into 246897a on scrogson:master.

@coveralls
Copy link

coveralls commented Oct 5, 2017

Coverage Status

Coverage increased (+0.6%) to 95.074% when pulling fa3c6a0 on chrislaskey:get-token-error into 246897a on scrogson:master.

@scrogson
Copy link
Member

scrogson commented Oct 6, 2017

❤️ 💚 💙 💛 💜

@scrogson scrogson merged commit 6d1808c into ueberauth:master Oct 6, 2017
@barthez barthez mentioned this pull request Nov 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants