-
Notifications
You must be signed in to change notification settings - Fork 241
feat: properly support error capturing #1075
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
Changes from 20 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
133fcb2
initial support for exception groups
giortzisg c183ee6
wip changes
giortzisg 00cf3e5
simplify error groups
giortzisg 7ed1116
reverse exception list
giortzisg 4412605
follow natural ordering
giortzisg 590f3f9
fix is_exception_group
giortzisg 263df5f
fix lint
giortzisg 9ee274c
uncap limit for chained errors
giortzisg 1a76769
Merge branch 'master' into feat/issue-grouping
giortzisg 967ed41
handle infinite recursion
giortzisg 1129e87
revert maxErrorDepth changes
giortzisg a403ec8
misc changes
giortzisg f608b91
Merge branch 'master' into feat/issue-grouping
giortzisg 9d59402
Merge branch 'master' into feat/issue-grouping
giortzisg 1a95964
skip wrapping error strings
giortzisg c03564e
add stacktrace on top level
giortzisg b02379c
Merge branch 'master' into feat/issue-grouping
giortzisg daa7813
fix: correctly add stacktrace to top level
giortzisg 8b2079d
skip on zero elements
giortzisg e852a0f
correctly add maxErrorDepth
giortzisg bde0402
fix: lint
giortzisg 36b0914
rename error_group to exception
giortzisg 4b8ab2c
document breaking changes
giortzisg d4a0726
Merge branch 'master' into feat/issue-grouping
giortzisg 533a87d
fix unwrap source & maxErrorDepth
giortzisg 359870d
Merge branch 'master' into feat/issue-grouping
giortzisg 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package sentry | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"slices" | ||
) | ||
|
||
const ( | ||
MechanismTypeGeneric string = "generic" | ||
MechanismTypeChained string = "chained" | ||
|
||
MechanismSourceCause string = "cause" | ||
) | ||
|
||
func convertErrorToExceptions(err error, maxErrorDepth int) []Exception { | ||
var exceptions []Exception | ||
visited := make(map[error]bool) | ||
convertErrorDFS(err, &exceptions, nil, "", visited, maxErrorDepth, 0) | ||
|
||
// mechanism type is used for debugging purposes, but since we can't really distinguish the origin of who invoked | ||
// captureException, we set it to nil if the error is not chained. | ||
if len(exceptions) == 1 { | ||
exceptions[0].Mechanism = nil | ||
} | ||
|
||
slices.Reverse(exceptions) | ||
|
||
// Add a trace of the current stack to the top level(outermost) error in a chain if | ||
// it doesn't have a stack trace yet. | ||
// We only add to the most recent error to avoid duplication and because the | ||
// current stack is most likely unrelated to errors deeper in the chain. | ||
if len(exceptions) > 0 && exceptions[len(exceptions)-1].Stacktrace == nil { | ||
exceptions[len(exceptions)-1].Stacktrace = NewStacktrace() | ||
} | ||
|
||
return exceptions | ||
} | ||
|
||
func convertErrorDFS(err error, exceptions *[]Exception, parentID *int, source string, visited map[error]bool, maxErrorDepth int, currentDepth int) { | ||
if err == nil { | ||
return | ||
} | ||
|
||
if visited[err] { | ||
return | ||
} | ||
visited[err] = true | ||
|
||
var isExceptionGroup bool | ||
|
||
switch err.(type) { | ||
case interface{ Unwrap() []error }: | ||
isExceptionGroup = true | ||
} | ||
|
||
exception := Exception{ | ||
Value: err.Error(), | ||
Type: reflect.TypeOf(err).String(), | ||
Stacktrace: ExtractStacktrace(err), | ||
} | ||
|
||
currentID := len(*exceptions) | ||
|
||
var mechanismType string | ||
|
||
if parentID == nil { | ||
mechanismType = MechanismTypeGeneric | ||
source = "" | ||
} else { | ||
mechanismType = MechanismTypeChained | ||
} | ||
|
||
exception.Mechanism = &Mechanism{ | ||
Type: mechanismType, | ||
ExceptionID: currentID, | ||
ParentID: parentID, | ||
Source: source, | ||
IsExceptionGroup: isExceptionGroup, | ||
} | ||
|
||
*exceptions = append(*exceptions, exception) | ||
|
||
if maxErrorDepth >= 0 && currentDepth >= maxErrorDepth { | ||
return | ||
} | ||
|
||
switch v := err.(type) { | ||
case interface{ Unwrap() []error }: | ||
unwrapped := v.Unwrap() | ||
for i := range unwrapped { | ||
if unwrapped[i] != nil { | ||
childSource := fmt.Sprintf("errors[%d]", i) | ||
convertErrorDFS(unwrapped[i], exceptions, ¤tID, childSource, visited, maxErrorDepth, currentDepth+1) | ||
} | ||
} | ||
case interface{ Unwrap() error }: | ||
unwrapped := v.Unwrap() | ||
if unwrapped != nil { | ||
convertErrorDFS(unwrapped, exceptions, ¤tID, MechanismSourceCause, visited, maxErrorDepth, currentDepth+1) | ||
} | ||
case interface{ Cause() error }: | ||
cause := v.Cause() | ||
if cause != nil { | ||
convertErrorDFS(cause, exceptions, ¤tID, MechanismSourceCause, visited, maxErrorDepth, currentDepth+1) | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.