Skip to content

Add missing cond for frames #787

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

Merged
merged 4 commits into from
Sep 9, 2024
Merged

Conversation

savhappy
Copy link
Collaborator

@savhappy savhappy commented Sep 6, 2024

-render_exception was missing a conditional and nil was being passed to Enum.map causing a Protocol.Undefined error
-closes #786

@@ -289,7 +289,7 @@ defmodule Sentry.Client do
|> Map.from_struct()
|> update_if_present(:mechanism, &Map.from_struct/1)
|> update_if_present(:stacktrace, fn %Interfaces.Stacktrace{frames: frames} ->
%{frames: Enum.map(frames, &Map.from_struct/1)}
%{frames: frames && Enum.map(frames, &Map.from_struct/1)}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should frames be present at all if it's null, according to Sentry docs?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@savhappy need to figure this ☝️ out before merging 🙃

Copy link
Collaborator Author

@savhappy savhappy Sep 7, 2024

Choose a reason for hiding this comment

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

Yep yep did some digging in the docs and it says the frames attribute is required if stacktrace is present so we should remove the property entirely if frames: nil.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice, perfect!

@whatyouhide
Copy link
Collaborator

We also need a test for this 🙃

Comment on lines 357 to 369
defp update_if_present(map, :stacktrace, fun) do
case Map.pop(map, :stacktrace) do
{nil, _} ->
map

{value, map} ->
if is_nil(value.frames) || value.frames == [] do
Map.delete(map, :stacktrace)
else
Map.put(map, :stacktrace, fun.(value))
end
end
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's not special-case update_if_present/3 to handle :stacktrace. In general, that leads to somewhat-confusing code because then function behaves differently based on the key, so it's effectively two functions.

You can do this instead:

case Map.from_struct(thread) do
  %{stacktrace: %{frames: [_ | _]} = stacktrace} ->
    %{stacktrace | frames: Enum.map(frames, &Map.from_struct/1)}

  thread_as_map ->
    Map.delete(thread_as_map, :stacktrace)
end

This is special-cased to handle :stacktrace and the :frames logic specifically, reads pretty clear IMO.

@savhappy savhappy requested a review from whatyouhide September 9, 2024 08:37
@@ -285,20 +285,24 @@ defmodule Sentry.Client do
end

defp render_exception(%Interfaces.Exception{} = exception) do
exception
|> Map.from_struct()
case Map.from_struct(exception) do
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok I missed that this was done in two places. Let's get this case code and extract it into a render_stacktrace function then!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done!

@savhappy savhappy force-pushed the sav/frames-missing-condition branch from a55605e to ffb8f3e Compare September 9, 2024 10:56
@savhappy savhappy requested a review from whatyouhide September 9, 2024 10:58
@whatyouhide whatyouhide merged commit 211a29f into master Sep 9, 2024
4 checks passed
@whatyouhide whatyouhide deleted the sav/frames-missing-condition branch September 9, 2024 11:02
whatyouhide added a commit that referenced this pull request Sep 9, 2024
@whatyouhide
Copy link
Collaborator

The fix here was incorrect, my bad. We were overriding the exception map completely. Tests were missing. I fixed this in d79919f @savhappy.

@savhappy
Copy link
Collaborator Author

savhappy commented Sep 9, 2024

Ah I see that! Thank you for the correction and the updated tests!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GenServer {Sentry.Transport.SenderRegistry, 6} terminating
2 participants