Debian Codegraph @

errors.go

raw symbols SCIP
1 package errors
2
3 import (
4 "errors"
5 "fmt"
6 "io"
7 "strings"
8 "sync/atomic"
9 )
10
11 // Export a number of functions or variables from package errors.
12 var (
13 As = errors.As
14 Is = errors.Is
15 Unwrap = errors.Unwrap
16 //Join = errors.Join
17 )
18
19 // includeBacktrace is a static variable that decides if when creating an error we store the backtrace with it.
20 var includeBacktrace atomic.Bool
21
22 func SetIncludeBacktrace(v bool) {
23 includeBacktrace.Store(v)
24 }
25
26 // Err is our custom error type that can store backtrace, file and line number
27 type Err struct {
28 m string
29 c error
30 t stack
31 }
32
33 func (e Err) Format(s fmt.State, verb rune) {
34 switch verb {
35 case 's':
36 io.WriteString(s, e.m)
37 switch {
38 case s.Flag('+'):
39 if e.c != nil {
40 io.WriteString(s, ": ")
41 io.WriteString(s, fmt.Sprintf("%+s", e.c))
42 }
43 }
44 case 'v':
45 e.Format(s, 's')
46 switch {
47 case s.Flag('+'):
48 if e.t != nil {
49 io.WriteString(s, "\n\t")
50 e.t.Format(s, 'v')
51 }
52 }
53 }
54 }
55
56 // Error implements the error interface
57 func (e Err) Error() string {
58 if includeBacktrace.Load() {
59 return e.m
60 }
61 s := strings.Builder{}
62 s.WriteString(e.m)
63 if ch := errors.Unwrap(e); ch != nil {
64 s.WriteString(": ")
65 s.WriteString(ch.Error())
66 }
67 return s.String()
68 }
69
70 // Unwrap implements the errors.Wrapper interface
71 func (e Err) Unwrap() error {
72 return e.c
73 }
74
75 // StackTrace returns the stack trace as returned by the debug.Stack function
76 func (e Err) StackTrace() StackTrace {
77 return e.t.StackTrace()
78 }
79
80 // Annotatef wraps an error with new message
81 func Annotatef(e error, s string, args ...interface{}) *Err {
82 err := wrap(e, s, args...)
83 return &err
84 }
85
86 // Newf creaates a new error
87 func Newf(s string, args ...interface{}) *Err {
88 err := wrap(nil, s, args...)
89 return &err
90 }
91
92 // Errorf is an alias for Newf
93 func Errorf(s string, args ...interface{}) error {
94 err := wrap(nil, s, args...)
95 return &err
96 }
97
98 func (e Err) Is(err interface{}) bool {
99 switch err.(type) {
100 case **Err:
101 return true
102 case *Err:
103 return true
104 case Err:
105 return true
106 default:
107 return false
108 }
109 }
110
111 // As implements support for errors.As
112 func (e *Err) As(err interface{}) bool {
113 switch x := err.(type) {
114 case **Err:
115 *x = e
116 case *Err:
117 *x = *e
118 case Err:
119 x = *e
120 default:
121 return false
122 }
123 return true
124 }
125
126 type StackTracer interface {
127 StackTrace() StackTrace
128 }
129
130 // ancestorOfCause returns true if the caller looks to be an ancestor of the given stack
131 // trace. We check this by seeing whether our stack prefix-matches the cause stack, which
132 // should imply the error was generated directly from our goroutine.
133 func ancestorOfCause(ourStack stack, causeStack StackTrace) bool {
134 // Stack traces are ordered such that the deepest frame is first. We'll want to check
135 // for prefix matching in reverse.
136 //
137 // As an example, imagine we have a prefix-matching stack for ourselves:
138 // [
139 // "github.com/go-ap/processing/processing.Validate",
140 // "testing.tRunner",
141 // "runtime.goexit"
142 // ]
143 //
144 // We'll want to compare this against an error cause that will have happened further
145 // down the stack. An example stack trace from such an error might be:
146 // [
147 // "github.com/go-ap/errors/errors.New",
148 // "testing.tRunner",
149 // "runtime.goexit"
150 // ]
151 //
152 // Their prefix matches, but we'll have to handle the match carefully as we need to match
153 // from back to forward.
154
155 // We can't possibly prefix match if our stack is larger than the cause stack.
156 if len(ourStack) > len(causeStack) {
157 return false
158 }
159
160 // We know the sizes are compatible, so compare program counters from back to front.
161 for idx := 0; idx < len(ourStack); idx++ {
162 if ourStack[len(ourStack)-1] != (uintptr)(causeStack[len(causeStack)-1]) {
163 return false
164 }
165 }
166
167 return true
168 }
169
170 func wrap(e error, s string, args ...interface{}) Err {
171 err := Err{
172 c: e,
173 m: fmt.Sprintf(s, args...),
174 }
175 if includeBacktrace.Load() {
176 causeStackTracer := new(StackTracer)
177 // If our cause has set a stack trace, and that trace is a child of our own function
178 // as inferred by prefix matching our current program counter stack, then we only want
179 // to decorate the error message rather than add a redundant stack trace.
180 stack := callers(2)
181 if !(As(e, causeStackTracer) && ancestorOfCause(*stack, (*causeStackTracer).StackTrace())) {
182 err.t = *stack
183 }
184 }
185 return err
186 }