Skip to content

Commit 740c825

Browse files
paulanthonywilsondoomspork
authored andcommitted
Improve configuration error messages (#27)
* Get Github config with Application.fetch_env! Gives a bit more of a useful error message if the user has messed up and not added that config correctly, as opposed to "no function clause matching in Keyword.merge/2". * Checking configuration for required keys Checks for the presence of client_id and client_secret. This may be a step to far. * Code style change
1 parent 41b2483 commit 740c825

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lib/ueberauth/strategy/github/oauth.ex

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ defmodule Ueberauth.Strategy.Github.OAuth do
2828
These options are only useful for usage outside the normal callback phase of Ueberauth.
2929
"""
3030
def client(opts \\ []) do
31-
config = Application.get_env(:ueberauth, Ueberauth.Strategy.Github.OAuth)
31+
config =
32+
:ueberauth
33+
|> Application.fetch_env!(Ueberauth.Strategy.Github.OAuth)
34+
|> check_config_key_exists(:client_id)
35+
|> check_config_key_exists(:client_secret)
36+
3237
client_opts =
3338
@defaults
3439
|> Keyword.merge(config)
@@ -73,4 +78,14 @@ defmodule Ueberauth.Strategy.Github.OAuth do
7378
|> put_header("Accept", "application/json")
7479
|> OAuth2.Strategy.AuthCode.get_token(params, headers)
7580
end
81+
82+
defp check_config_key_exists(config, key) when is_list(config) do
83+
unless Keyword.has_key?(config, key) do
84+
raise "#{inspect (key)} missing from config :ueberauth, Ueberauth.Strategy.Github"
85+
end
86+
config
87+
end
88+
defp check_config_key_exists(_, _) do
89+
raise "Config :ueberauth, Ueberauth.Strategy.Github is not a keyword list, as expected"
90+
end
7691
end

0 commit comments

Comments
 (0)