Skip to content

Commit

Permalink
Improve "mix sentry.send_test_event" (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide committed Dec 9, 2023
1 parent 17985cf commit 6281925
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions lib/mix/tasks/sentry.send_test_event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,23 @@ defmodule Mix.Tasks.Sentry.SendTestEvent do
## Options
* `--no-compile` - does not compile, even if files require compilation.
* `--type` - `exception` or `message`. Defaults to `exception`. *Available since v10.1.0.*.
* `--no-stacktrace` - does not include a stacktrace in the reported event. *Available since
v10.1.0.*.
"""

@switches [
compile: :boolean,
stacktrace: :boolean,
type: :string
]

@impl true
def run(args) when is_list(args) do
unless "--no-compile" in args do
{opts, args} = OptionParser.parse!(args, strict: @switches)

if Keyword.get(opts, :compile, true) do
Mix.Task.run("compile", args)
end

Expand All @@ -34,7 +45,7 @@ defmodule Mix.Tasks.Sentry.SendTestEvent do
print_environment_info()

if Config.dsn() do
send_event()
send_event(opts)
else
Mix.shell().info([
:yellow,
Expand All @@ -57,13 +68,31 @@ defmodule Mix.Tasks.Sentry.SendTestEvent do
Mix.shell().info("hackney_opts: #{inspect(Config.hackney_opts())}\n")
end

defp send_event do
defp send_event(opts) do
stacktrace_opts =
if Keyword.get(opts, :stacktrace, true) do
{:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
[stacktrace: stacktrace]
else
[]
end

Mix.shell().info("Sending test event...")

exception = %RuntimeError{message: "Testing sending Sentry event"}
{:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
result =
case Keyword.get(opts, :type, "exception") do
"exception" ->
exception = %RuntimeError{message: "Testing sending Sentry event"}
Sentry.capture_exception(exception, [result: :sync] ++ stacktrace_opts)

"message" ->
Sentry.capture_message(
"Testing sending Sentry event",
[result: :sync] ++ stacktrace_opts
)
end

case Sentry.capture_exception(exception, result: :sync, stacktrace: stacktrace) do
case result do
{:ok, id} ->
Mix.shell().info([:green, :bright, "Test event sent", :reset, "\nEvent ID: #{id}"])

Expand Down

0 comments on commit 6281925

Please sign in to comment.