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 1 commit
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
2 changes: 1 addition & 1 deletion lib/sentry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ defmodule Sentry do
Client.send_event(event, opts)

!Config.dsn() ->
:ignored
Client.validate_and_ignore(opts)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is gonna make it harder to track the return value–I have to jump to another module. Maybe we can do

Client.validate_options!(opts)
:ignored

instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good idea!


included_envs == :all or to_string(Config.environment_name()) in included_envs ->
Client.send_event(event, opts)
Expand Down
6 changes: 6 additions & 0 deletions lib/sentry/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ defmodule Sentry.Client do
end
end

@spec validate_and_ignore(keyword()) :: :ignored
def validate_and_ignore(opts) when is_list(opts) do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also re-use this function above where we call NimbleOptions.validate! if we change it to return the options, like suggested in the other comment.

_opts = NimbleOptions.validate!(opts, @send_event_opts_schema)
:ignored
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_and_ignore(client: [bad_key: :nada])
end)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
assert_raise(NimbleOptions.ValidationError, fn ->
Sentry.Client.validate_and_ignore(client: [bad_key: :nada])
end)
assert_raise NimbleOptions.ValidationError, fn ->
Sentry.Client.validate_and_ignore(client: [bad_key: :nada])
end


assert :ignored == Sentry.Client.validate_and_ignore(client: :hackney)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, but matches other tests:

Suggested change
assert :ignored == Sentry.Client.validate_and_ignore(client: :hackney)
assert :ignored = Sentry.Client.validate_and_ignore(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