-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors2.go
More file actions
47 lines (38 loc) · 1018 Bytes
/
errors2.go
File metadata and controls
47 lines (38 loc) · 1018 Bytes
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
40
41
42
43
44
45
46
47
// Package errors is a minimal wrapper around the standard library errors package.
// It changes the API from New(msg string) and fmt.Errorf(format string, args ...any)
// to unified New(format string, args ...any) and Wrap(err error, format string, args ...any) functions.
package errors
import (
"errors"
"fmt"
)
type (
wrap struct {
err error
msg string
}
)
func Is(err, target error) bool { return errors.Is(err, target) }
func As(err error, target any) bool { return errors.As(err, target) }
func Unwrap(err error) error { return errors.Unwrap(err) }
func New(format string, args ...any) error {
if args == nil {
return errors.New(format)
}
return fmt.Errorf(format, args...)
}
func Wrap(err error, format string, args ...any) error {
if err == nil {
panic("wrapping nil error")
}
return &wrap{
err: err,
msg: fmt.Sprintf(format, args...),
}
}
func (w *wrap) Error() string {
return fmt.Sprintf("%v: %v", w.msg, w.err)
}
func (w *wrap) Unwrap() error {
return w.err
}