Skip to content

Commit 1200932

Browse files
committed
back to the roots
1 parent 5ebd3bd commit 1200932

17 files changed

+140
-895
lines changed

README.md

+8-37
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,15 @@
77

88
# errors
99

10-
Stdlib `errors` package extension. `go1.13` `errors.Is` and `errors.As` are the same functions as in stdlib (not even copies).
10+
`errors` is a wrapper around the standard `errors.New` and `fmt.Errorf` functions.
11+
It unifies their APIs, allowing you to create new errors or wrap existing ones with rich context (formatted message with arguments).
1112

12-
```go
13-
// as usual
14-
err = errors.New("msg")
13+
It provides two core functions:
1514

16-
// do not capture caller info
17-
err = errors.NewNoLoc("msg")
18-
19-
// fmt.Sprintf like
20-
err = errors.New("message %v", "args")
21-
22-
// one Frame higher
23-
err = errors.NewDepth(1, "msg")
24-
25-
// the same result as previous
26-
pc := loc.Caller(1)
27-
err = errors.NewLoc(pc, "msg")
28-
29-
// Wrap error
30-
err = errors.Wrap(err, "msg %v", "args")
31-
32-
// all the same function types are available
33-
err = errors.WrapNoLoc(err, "msg")
34-
35-
err = errors.WrapDepth(err, 1, "msg %v", "args")
36-
37-
err = errors.WrapLoc(err, pc, "msg %v", "args")
3815
```
39-
40-
## Caller
41-
42-
Caller frame can be added to error so later you can get to know where error was generated. It's added by default and captures instruction calling `errors.(Wrap|New)*`.
43-
44-
Caller is moved to a separate module [tlog.app/go/loc](https://github.com/tlog-dev/loc).
45-
46-
```go
47-
pc := loc.Caller(1)
48-
49-
pc = loc.FuncEntry(1)
16+
errors.New(format string, args ...any) error
17+
errors.Wrap(err error, format string, args ...any) error
5018
```
19+
20+
While it previously had more features, I eventually realized that,
21+
in most cases, anything beyond simple error wrapping is an overcomplication.

errors.go

-234
This file was deleted.

errors2.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// errors is a tiny stdlib errors wrapper.
2+
// All it does changes the api from New(msg string) and fmt.Errorf(format string, args ...any) to New(format string, args ...any).
3+
package errors
4+
5+
import (
6+
"errors"
7+
"fmt"
8+
)
9+
10+
type (
11+
wrap struct {
12+
err error
13+
msg string
14+
}
15+
)
16+
17+
func Is(err, target error) bool { return errors.Is(err, target) }
18+
func As(err error, target any) bool { return errors.As(err, target) }
19+
func Unwrap(err error) error { return errors.Unwrap(err) }
20+
21+
func New(format string, args ...any) error {
22+
if args == nil {
23+
return errors.New(format)
24+
}
25+
26+
return fmt.Errorf(format, args...)
27+
}
28+
29+
func Wrap(err error, format string, args ...any) error {
30+
if err == nil {
31+
panic("wrapping nil error")
32+
}
33+
34+
return &wrap{
35+
err: err,
36+
msg: fmt.Sprintf(format, args...),
37+
}
38+
}
39+
40+
func (w *wrap) Error() string {
41+
return fmt.Sprintf("%v: %v", w.msg, w.err)
42+
}
43+
44+
func (w *wrap) Unwrap() error {
45+
return w.err
46+
}

0 commit comments

Comments
 (0)