|
| 1 | +package sentry |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "slices" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + MechanismTypeGeneric string = "generic" |
| 11 | + MechanismTypeChained string = "chained" |
| 12 | + MechanismTypeUnwrap string = "unwrap" |
| 13 | + MechanismSourceCause string = "cause" |
| 14 | +) |
| 15 | + |
| 16 | +type visited struct { |
| 17 | + comparable map[error]struct{} |
| 18 | + msgs map[string]struct{} |
| 19 | +} |
| 20 | + |
| 21 | +func (v *visited) seenError(err error) bool { |
| 22 | + t := reflect.TypeOf(err) |
| 23 | + if t == nil { |
| 24 | + return false |
| 25 | + } |
| 26 | + |
| 27 | + if t.Comparable() { |
| 28 | + if _, ok := v.comparable[err]; ok { |
| 29 | + return true |
| 30 | + } |
| 31 | + v.comparable[err] = struct{}{} |
| 32 | + return false |
| 33 | + } |
| 34 | + |
| 35 | + key := t.String() + err.Error() |
| 36 | + if _, ok := v.msgs[key]; ok { |
| 37 | + return true |
| 38 | + } |
| 39 | + v.msgs[key] = struct{}{} |
| 40 | + return false |
| 41 | +} |
| 42 | + |
| 43 | +func convertErrorToExceptions(err error, maxErrorDepth int) []Exception { |
| 44 | + var exceptions []Exception |
| 45 | + vis := &visited{ |
| 46 | + make(map[error]struct{}), |
| 47 | + make(map[string]struct{}), |
| 48 | + } |
| 49 | + convertErrorDFS(err, &exceptions, nil, "", vis, maxErrorDepth, 0) |
| 50 | + |
| 51 | + // mechanism type is used for debugging purposes, but since we can't really distinguish the origin of who invoked |
| 52 | + // captureException, we set it to nil if the error is not chained. |
| 53 | + if len(exceptions) == 1 { |
| 54 | + exceptions[0].Mechanism = nil |
| 55 | + } |
| 56 | + |
| 57 | + slices.Reverse(exceptions) |
| 58 | + |
| 59 | + // Add a trace of the current stack to the top level(outermost) error in a chain if |
| 60 | + // it doesn't have a stack trace yet. |
| 61 | + // We only add to the most recent error to avoid duplication and because the |
| 62 | + // current stack is most likely unrelated to errors deeper in the chain. |
| 63 | + if len(exceptions) > 0 && exceptions[len(exceptions)-1].Stacktrace == nil { |
| 64 | + exceptions[len(exceptions)-1].Stacktrace = NewStacktrace() |
| 65 | + } |
| 66 | + |
| 67 | + return exceptions |
| 68 | +} |
| 69 | + |
| 70 | +func convertErrorDFS(err error, exceptions *[]Exception, parentID *int, source string, visited *visited, maxErrorDepth int, currentDepth int) { |
| 71 | + if err == nil { |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + if visited.seenError(err) { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + _, isExceptionGroup := err.(interface{ Unwrap() []error }) |
| 80 | + |
| 81 | + exception := Exception{ |
| 82 | + Value: err.Error(), |
| 83 | + Type: reflect.TypeOf(err).String(), |
| 84 | + Stacktrace: ExtractStacktrace(err), |
| 85 | + } |
| 86 | + |
| 87 | + currentID := len(*exceptions) |
| 88 | + |
| 89 | + var mechanismType string |
| 90 | + |
| 91 | + if parentID == nil { |
| 92 | + mechanismType = MechanismTypeGeneric |
| 93 | + source = "" |
| 94 | + } else { |
| 95 | + mechanismType = MechanismTypeChained |
| 96 | + } |
| 97 | + |
| 98 | + exception.Mechanism = &Mechanism{ |
| 99 | + Type: mechanismType, |
| 100 | + ExceptionID: currentID, |
| 101 | + ParentID: parentID, |
| 102 | + Source: source, |
| 103 | + IsExceptionGroup: isExceptionGroup, |
| 104 | + } |
| 105 | + |
| 106 | + *exceptions = append(*exceptions, exception) |
| 107 | + |
| 108 | + if maxErrorDepth >= 0 && currentDepth >= maxErrorDepth { |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + switch v := err.(type) { |
| 113 | + case interface{ Unwrap() []error }: |
| 114 | + unwrapped := v.Unwrap() |
| 115 | + for i := range unwrapped { |
| 116 | + if unwrapped[i] != nil { |
| 117 | + childSource := fmt.Sprintf("errors[%d]", i) |
| 118 | + convertErrorDFS(unwrapped[i], exceptions, ¤tID, childSource, visited, maxErrorDepth, currentDepth+1) |
| 119 | + } |
| 120 | + } |
| 121 | + case interface{ Unwrap() error }: |
| 122 | + unwrapped := v.Unwrap() |
| 123 | + if unwrapped != nil { |
| 124 | + convertErrorDFS(unwrapped, exceptions, ¤tID, MechanismTypeUnwrap, visited, maxErrorDepth, currentDepth+1) |
| 125 | + } |
| 126 | + case interface{ Cause() error }: |
| 127 | + cause := v.Cause() |
| 128 | + if cause != nil { |
| 129 | + convertErrorDFS(cause, exceptions, ¤tID, MechanismSourceCause, visited, maxErrorDepth, currentDepth+1) |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments