Skip to content

Commit dc203bf

Browse files
committed
change CompileError to ArgumentError. Update error message
1 parent 952ce9b commit dc203bf

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

lib/elixir/lib/inspect.ex

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ defimpl Inspect, for: Any do
455455
only = Keyword.get(options, :only, fields)
456456
except = Keyword.get(options, :except, [])
457457

458-
:ok = validate_option(only, fields, module, __CALLER__)
459-
:ok = validate_option(except, fields, module, __CALLER__)
458+
:ok = validate_option(only, fields, module)
459+
:ok = validate_option(except, fields, module)
460460

461461
filtered_fields =
462462
fields
@@ -481,13 +481,15 @@ defimpl Inspect, for: Any do
481481
end
482482
end
483483

484-
defp validate_option(option_list, fields, module, caller) do
485-
if not Enum.empty?(option_list -- fields) do
486-
description = "When deriving inspect protocol of #{module}, values must match struct fields"
487-
raise CompileError, file: caller.file, line: caller.line, description: description
488-
end
484+
defp validate_option(option_list, fields, module) do
485+
case option_list -- fields do
486+
[] ->
487+
:ok
489488

490-
:ok
489+
unknown_fields ->
490+
raise ArgumentError,
491+
"unknown fields #{Kernel.inspect(unknown_fields)} given when deriving the Inspect protocol for #{Kernel.inspect(module)}. :only and :except values must match struct fields"
492+
end
491493
end
492494

493495
def inspect(%module{} = struct, opts) do

lib/elixir/test/elixir/inspect_test.exs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -695,27 +695,25 @@ defmodule Inspect.MapTest do
695695
end
696696

697697
test "struct missing fields in the :only option" do
698-
message =
699-
~r"When deriving inspect protocol of Elixir.Inspect.MapTest.StructMissingFieldsInOnlyOption, values must match struct fields"
700-
701-
assert_raise CompileError, message, fn ->
702-
defmodule StructMissingFieldsInOnlyOption do
703-
@derive {Inspect, only: [:c]}
704-
defstruct [:a, :b]
705-
end
706-
end
698+
assert_raise ArgumentError,
699+
"unknown fields [:c] given when deriving the Inspect protocol for Inspect.MapTest.StructMissingFieldsInOnlyOption. :only and :except values must match struct fields",
700+
fn ->
701+
defmodule StructMissingFieldsInOnlyOption do
702+
@derive {Inspect, only: [:c]}
703+
defstruct [:a, :b]
704+
end
705+
end
707706
end
708707

709708
test "struct missing fields in the :except option" do
710-
message =
711-
~r"When deriving inspect protocol of Elixir.Inspect.MapTest.StructMissingFieldsInExceptOption, values must match struct fields"
712-
713-
assert_raise CompileError, message, fn ->
714-
defmodule StructMissingFieldsInExceptOption do
715-
@derive {Inspect, except: [:c]}
716-
defstruct [:a, :b]
717-
end
718-
end
709+
assert_raise ArgumentError,
710+
"unknown fields [:c, :d] given when deriving the Inspect protocol for Inspect.MapTest.StructMissingFieldsInExceptOption. :only and :except values must match struct fields",
711+
fn ->
712+
defmodule StructMissingFieldsInExceptOption do
713+
@derive {Inspect, except: [:c, :d]}
714+
defstruct [:a, :b]
715+
end
716+
end
719717
end
720718

721719
defmodule StructWithExceptOption do

0 commit comments

Comments
 (0)