This repository was archived by the owner on Aug 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 30
feat stacktraces and exceptions #61
Merged
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ccb80f6
created MainEventProcessor
a98da12
WIP
ee4f571
WIP
7733648
fix merge conflict
199de95
fixing protocol names
affa3cb
parsing stacktraces
13d48b7
WIP
9f09714
removed frame cache
ea0cebe
cleaning up
9a62459
fix merge conflict
69aea15
ref stacktraces and exceptions
b0ef08a
code review
marandaneto 224a099
Merge branch 'master' into feat/stacktraces
marandaneto a7ff3aa
fix assert
marandaneto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
sentry-core/src/main/java/io/sentry/core/MainEventProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package io.sentry.core; | ||
|
|
||
| import io.sentry.core.exception.SentryExceptionReader; | ||
| import io.sentry.core.protocol.Message; | ||
| import io.sentry.core.protocol.SentryException; | ||
| import io.sentry.core.protocol.SentryStackFrame; | ||
| import io.sentry.core.protocol.SentryStackTrace; | ||
| import io.sentry.core.util.Objects; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class MainEventProcessor implements EventProcessor { | ||
|
|
||
| private final SentryOptions options; | ||
|
|
||
| public MainEventProcessor(SentryOptions options) { | ||
| this.options = Objects.requireNonNull(options, "The SentryOptions is required."); | ||
| } | ||
|
|
||
| @Override | ||
| public SentryEvent process(SentryEvent event) { | ||
| Throwable throwable = event.getThrowable(); | ||
| if (throwable != null) { | ||
|
|
||
| if (event.getMessage() == null) { | ||
| event.setMessage(getMessage(throwable)); | ||
| } | ||
|
|
||
| event.setException(SentryExceptionReader.sentryExceptionReader(throwable)); | ||
| } | ||
|
|
||
| return event; | ||
| } | ||
|
|
||
| private Message getMessage(Throwable throwable) { | ||
| Message message = new Message(); | ||
| message.setFormatted(throwable.getMessage()); | ||
| return message; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
sentry-core/src/main/java/io/sentry/core/exception/ExceptionMechanismThrowable.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package io.sentry.core.exception; | ||
|
|
||
| import io.sentry.core.protocol.Mechanism; | ||
|
|
||
| /** | ||
| * A throwable decorator that holds an {@link io.sentry.core.protocol.Mechanism} related to the decorated {@link Throwable}. | ||
| */ | ||
| public final class ExceptionMechanismThrowable extends Throwable { | ||
| private static final long serialVersionUID = 100L; | ||
|
|
||
| private final Mechanism exceptionMechanism; | ||
| private final Throwable throwable; | ||
|
|
||
| /** | ||
| * A {@link Throwable} that decorates another with a Sentry {@link Mechanism}. | ||
| * @param mechanism The {@link Mechanism}. | ||
| * @param throwable The {@link java.lang.Throwable}. | ||
| */ | ||
| public ExceptionMechanismThrowable(Mechanism mechanism, Throwable throwable) { | ||
| this.exceptionMechanism = mechanism; | ||
| this.throwable = throwable; | ||
| } | ||
|
|
||
| public Mechanism getExceptionMechanism() { | ||
| return exceptionMechanism; | ||
| } | ||
|
|
||
| public Throwable getThrowable() { | ||
| return throwable; | ||
| } | ||
| } |
111 changes: 111 additions & 0 deletions
111
sentry-core/src/main/java/io/sentry/core/exception/SentryExceptionReader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package io.sentry.core.exception; | ||
|
|
||
| import io.sentry.core.protocol.Mechanism; | ||
| import io.sentry.core.protocol.SentryException; | ||
| import io.sentry.core.protocol.SentryStackTrace; | ||
|
|
||
| import java.util.ArrayDeque; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Deque; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| public final class SentryExceptionReader { | ||
|
|
||
| /** | ||
| * Creates a new instance from the given {@code throwable}. | ||
| * | ||
| * @param throwable the {@link Throwable} to build this instance from | ||
| */ | ||
| public static List<SentryException> sentryExceptionReader(final Throwable throwable) { | ||
| return sentryExceptionReader(extractExceptionQueue(throwable)); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new instance from the given {@code exceptions}. | ||
| * | ||
| * @param exceptions a {@link Deque} of {@link SentryException} to build this instance from | ||
| */ | ||
| private static List<SentryException> sentryExceptionReader(final Deque<SentryException> exceptions) { | ||
| return new ArrayList<>(exceptions); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a Sentry exception based on a Java Throwable. | ||
| * <p> | ||
| * The {@code childExceptionStackTrace} parameter is used to define the common frames with the child exception | ||
| * (Exception caused by {@code throwable}). | ||
| * | ||
| * @param throwable Java exception to send to Sentry. | ||
| * @param childExceptionStackTrace StackTrace of the exception caused by {@code throwable}. | ||
| * @param exceptionMechanism The optional {@link Mechanism} of the {@code throwable}. | ||
| * Or null if none exist. | ||
| */ | ||
| private static SentryException sentryExceptionReader( | ||
| Throwable throwable, | ||
| StackTraceElement[] childExceptionStackTrace, // TODO: do we need that? | ||
marandaneto marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Mechanism exceptionMechanism) { | ||
|
|
||
| Package exceptionPackage = throwable.getClass().getPackage(); | ||
| String fullClassName = throwable.getClass().getName(); | ||
|
|
||
| SentryException exception = new SentryException(); | ||
|
|
||
| // this.exceptionMessage = throwable.getMessage(); | ||
| String exceptionClassName = exceptionPackage != null | ||
| ? fullClassName.replace(exceptionPackage.getName() + ".", "") | ||
| : fullClassName; | ||
|
|
||
| String exceptionPackageName = exceptionPackage != null | ||
| ? exceptionPackage.getName() | ||
| : null; | ||
| // TODO: whats about those missing fields? message, classname, packagename, ... | ||
marandaneto marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| SentryStackTrace sentryStackTrace = new SentryStackTrace(); | ||
| sentryStackTrace.setFrames( | ||
| Arrays.asList(SentryStackFrameReader.fromStackTraceElements( | ||
| throwable.getStackTrace(), null))); // TODO: cached frames | ||
|
|
||
| exception.setStacktrace(sentryStackTrace); | ||
| exception.setType("ValueError"); // TODO ? | ||
marandaneto marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // exception.setValue(); type or value is mandatory | ||
|
|
||
| exception.setMechanism(exceptionMechanism); | ||
| return exception; | ||
| } | ||
|
|
||
| /** | ||
| * Transforms a {@link Throwable} into a Queue of {@link SentryException}. | ||
| * <p> | ||
| * Exceptions are stored in the queue from the most recent one to the oldest one. | ||
| * | ||
| * @param throwable throwable to transform in a queue of exceptions. | ||
| * @return a queue of exception with StackTrace. | ||
| */ | ||
| private static Deque<SentryException> extractExceptionQueue(Throwable throwable) { | ||
| Deque<SentryException> exceptions = new ArrayDeque<>(); | ||
| Set<Throwable> circularityDetector = new HashSet<>(); | ||
| StackTraceElement[] childExceptionStackTrace = new StackTraceElement[0]; | ||
| Mechanism exceptionMechanism = null; | ||
|
|
||
| //Stack the exceptions to send them in the reverse order | ||
| while (throwable != null && circularityDetector.add(throwable)) { | ||
| if (throwable instanceof ExceptionMechanismThrowable) { | ||
| ExceptionMechanismThrowable exceptionMechanismThrowable = (ExceptionMechanismThrowable) throwable; | ||
bruno-garcia marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| exceptionMechanism = exceptionMechanismThrowable.getExceptionMechanism(); | ||
| throwable = exceptionMechanismThrowable.getThrowable(); | ||
| } else { | ||
| exceptionMechanism = null; | ||
| } | ||
|
|
||
| SentryException exception = sentryExceptionReader(throwable, childExceptionStackTrace, exceptionMechanism); | ||
| exceptions.add(exception); | ||
| childExceptionStackTrace = throwable.getStackTrace(); | ||
| throwable = throwable.getCause(); | ||
| } | ||
|
|
||
| return exceptions; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.