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

Validate options in dev mode #772

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/sentry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ defmodule Sentry do
Client.send_event(event, opts)

!Config.dsn() ->
_opts = Client.validate_options!(opts)
:ignored

included_envs == :all or to_string(Config.environment_name()) in included_envs ->
Expand Down
7 changes: 6 additions & 1 deletion lib/sentry/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ defmodule Sentry.Client do
| :unsampled
| :excluded
def send_event(%Event{} = event, opts) when is_list(opts) do
opts = NimbleOptions.validate!(opts, @send_event_opts_schema)
opts = validate_options!(opts)

result_type = Keyword.get_lazy(opts, :result, &Config.send_result/0)
sample_rate = Keyword.get_lazy(opts, :sample_rate, &Config.sample_rate/0)
Expand Down Expand Up @@ -160,6 +160,11 @@ defmodule Sentry.Client do
end
end

@spec validate_options!(keyword()) :: keyword()
def validate_options!(opts) when is_list(opts) do
NimbleOptions.validate!(opts, @send_event_opts_schema)
end

defp sample_event(sample_rate) do
cond do
sample_rate == 1 -> :ok
Expand Down
10 changes: 10 additions & 0 deletions test/sentry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ defmodule SentryTest do
assert logged_count == 2
end

test "raises error with validate_and_ignore/1 in dev mode if opts passed are invalid " do
put_test_config(dsn: nil, test_mode: false)

assert_raise NimbleOptions.ValidationError, fn ->
Sentry.Client.validate_options!(client: [bad_key: :nada])
end

assert [client: :hackney] = Sentry.Client.validate_options!(client: :hackney)
end

test "does not send events if :dsn is not configured or nil (if not in test mode)" do
put_test_config(dsn: nil, test_mode: false)
event = Sentry.Event.transform_exception(%RuntimeError{message: "oops"}, [])
Expand Down