/
errors.go
40 lines (29 loc) · 908 Bytes
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package fmt
import (
"sync/atomic"
errorsinternal "github.com/golang-exp/errors-poc/internal/errors"
)
type errorFormatter struct {
StackLevel errorsinternal.StackLeveler
}
var defaultErrorFormatter atomic.Value
func init() {
defaultErrorFormatter.Store(new(errorFormatter))
}
func Default() *errorFormatter { return defaultErrorFormatter.Load().(*errorFormatter) }
func NewErrorFormatter(s errorsinternal.StackLeveler) *errorFormatter {
e := new(errorFormatter)
e.StackLevel = s
return e
}
func SetDefault(e *errorFormatter) {
defaultErrorFormatter.Store(e)
}
// THIS IS NOT USABLE - JUST FOR TESTING
func Errorf(format string, a ...any) error {
return errorsinternal.Errorf(Default().StackLevel, 4, format, a...)
}
// THIS IS NOT USABLE - JUST FOR TESTING
func (ef *errorFormatter) Errorf(format string, a ...any) error {
return errorsinternal.Errorf(ef.StackLevel, 4, format, a...)
}