Skip to content

Commit 95c65d4

Browse files
committed
Rename expected_key to key in MissingParamError
1 parent c2e2c84 commit 95c65d4

5 files changed

Lines changed: 28 additions & 13 deletions

File tree

lib/assent.ex

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,33 @@ defmodule Assent do
3434
end
3535

3636
defmodule MissingParamError do
37-
defexception [:expected_key, :params]
37+
defexception [:key, :params]
3838

3939
@type t :: %__MODULE__{
40-
expected_key: binary(),
40+
key: binary(),
4141
params: map()
4242
}
4343

44+
# TODO: Deprecated, remove in 0.3
45+
def exception(opts) do
46+
opts =
47+
case Keyword.fetch(opts, :expected_key) do
48+
{:ok, key} ->
49+
IO.warn("The `expected_key` option is deprecated. Please use `key` instead.")
50+
[key: key, params: opts[:params]]
51+
52+
:error ->
53+
opts
54+
end
55+
56+
struct!(__MODULE__, opts)
57+
end
58+
4459
def message(exception) do
45-
expected_key = inspect(exception.expected_key)
60+
key = inspect(exception.key)
4661
param_keys = exception.params |> Map.keys() |> Enum.sort() |> inspect()
4762

48-
"Expected #{expected_key} in params, got: #{param_keys}"
63+
"Expected #{key} in params, got: #{param_keys}"
4964
end
5065
end
5166

@@ -150,7 +165,7 @@ defmodule Assent do
150165
def fetch_param(params, key) when is_map(params) and is_binary(key) do
151166
case Map.fetch(params, key) do
152167
{:ok, value} -> {:ok, value}
153-
:error -> {:error, MissingParamError.exception(expected_key: key, params: params)}
168+
:error -> {:error, MissingParamError.exception(key: key, params: params)}
154169
end
155170
end
156171

test/assent/strategies/oauth2_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ defmodule Assent.Strategy.OAuth2Test do
188188
params = Map.delete(params, "state")
189189

190190
assert {:error, %MissingParamError{} = error} = OAuth2.callback(config, params)
191-
assert error.expected_key == "state"
191+
assert error.key == "state"
192192
end
193193

194194
test "with invalid `state` param", %{config: config, callback_params: params} do
@@ -233,7 +233,7 @@ defmodule Assent.Strategy.OAuth2Test do
233233
params = Map.delete(params, "code")
234234

235235
assert {:error, %MissingParamError{} = error} = OAuth2.callback(config, params)
236-
assert error.expected_key == "code"
236+
assert error.key == "code"
237237
end
238238

239239
test "with `code_verifier: true` with missing `:code_verifier` in session_params", %{

test/assent/strategies/oauth_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,15 +367,15 @@ defmodule Assent.Strategy.OAuthTest do
367367
params = Map.delete(params, "oauth_token")
368368

369369
assert {:error, %MissingParamError{} = error} = OAuth.callback(config, params)
370-
assert error.expected_key == "oauth_token"
370+
assert error.key == "oauth_token"
371371
assert error.params == %{"oauth_verifier" => "hfdp7dh39dks9884"}
372372
end
373373

374374
test "with missing `oauth_verifier` param", %{config: config, callback_params: params} do
375375
params = Map.delete(params, "oauth_verifier")
376376

377377
assert {:error, %MissingParamError{} = error} = OAuth.callback(config, params)
378-
assert error.expected_key == "oauth_verifier"
378+
assert error.key == "oauth_verifier"
379379
assert error.params == %{"oauth_token" => "hh5s93j4hdidpola"}
380380
end
381381

test/assent/strategies/telegram_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ defmodule Assent.Strategy.TelegramTest do
118118
assert {:error, %Assent.MissingParamError{} = error} =
119119
Telegram.callback(config, callback_params)
120120

121-
assert error.expected_key == "hash"
121+
assert error.key == "hash"
122122
end
123123

124124
@tag authorization_channel: :web_mini_app
@@ -131,7 +131,7 @@ defmodule Assent.Strategy.TelegramTest do
131131
assert {:error, %Assent.MissingParamError{} = error} =
132132
Telegram.callback(config, callback_params)
133133

134-
assert error.expected_key == "init_data"
134+
assert error.key == "init_data"
135135
end
136136

137137
test "with missing auth_date param", %{config: config, callback_params: callback_params} do
@@ -140,7 +140,7 @@ defmodule Assent.Strategy.TelegramTest do
140140
assert {:error, %Assent.MissingParamError{} = error} =
141141
Telegram.callback(config, callback_params)
142142

143-
assert error.expected_key == "auth_date"
143+
assert error.key == "auth_date"
144144
end
145145

146146
test "with expired auth_date param", %{config: config, callback_params: callback_params} do

test/assent_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ defmodule AssentTest do
1919
assert Assent.fetch_param(params, "a") == {:ok, 1}
2020

2121
assert {:error, %Assent.MissingParamError{} = error} = Assent.fetch_param(params, "c")
22-
assert error.expected_key == "c"
22+
assert error.key == "c"
2323
assert error.params == params
2424
assert Exception.message(error) == "Expected \"c\" in params, got: [\"a\", \"b\"]"
2525
end

0 commit comments

Comments
 (0)