Skip to content

Commit 9dfe262

Browse files
author
Dean Karn
authored
Enhancements (#12)
- Updated to make it a 100% drop-in replacement for the std Go library errors package - Updated to make error chain work with std Go errors Unwrap, As, and Is interfaces. It's 100% inter-operable with std Go wrapping. - Added RegisterErrorFormatFn to allow custom formatting of error output. - Optimized default error printing. - Updated output format to include full path to the file to clearly delineate where the error happened if the file name is the same.
1 parent e4cd6a5 commit 9dfe262

File tree

13 files changed

+477
-146
lines changed

13 files changed

+477
-146
lines changed

.github/workflows/go.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Lint & Test
2+
on: [push, pull_request]
3+
jobs:
4+
test:
5+
strategy:
6+
matrix:
7+
go-version: [1.14.x,1.13.x]
8+
platform: [ubuntu-latest, macos-latest, windows-latest]
9+
runs-on: ${{ matrix.platform }}
10+
steps:
11+
- name: Install Go
12+
uses: actions/setup-go@v1
13+
with:
14+
go-version: ${{ matrix.go-version }}
15+
16+
- name: Priming Cache
17+
uses: actions/cache@v1
18+
with:
19+
path: ~/go/pkg/mod
20+
key: ${{ runner.os }}-v1-go-${{ hashFiles('**/go.sum') }}
21+
restore-keys: |
22+
${{ runner.os }}-v1-go-
23+
24+
- name: Checkout code
25+
uses: actions/checkout@v2
26+
27+
- name: Lint
28+
if: matrix.platform == 'ubuntu-latest' && matrix.go-version == '1.14.x'
29+
run: |
30+
export PATH=$PATH:$(go env GOPATH)/bin # temporary fix. See https://github.com/actions/setup-go/issues/14
31+
go get -u golang.org/x/lint/golint
32+
golint -set_exit_status ./...
33+
34+
- name: Test
35+
run: go test ./...

.travis.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ test:
1313
$(GOCMD) test -cover -race ./...
1414

1515
bench:
16-
$(GOCMD) test -bench=. -benchmem ./...
16+
$(GOCMD) test -run=NONE -bench=. -benchmem ./...
1717

1818
.PHONY: test lint linters-install

README.md

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
Package errors
22
============
3-
![Project status](https://img.shields.io/badge/version-5.0.1-green.svg)
3+
![Project status](https://img.shields.io/badge/version-5.1.0-green.svg)
44
[![Build Status](https://travis-ci.org/go-playground/errors.svg?branch=master)](https://travis-ci.org/go-playground/errors)
55
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/errors)](https://goreportcard.com/report/github.com/go-playground/errors)
6-
[![GoDoc](https://godoc.org/github.com/go-playground/errors?status.svg)](https://godoc.org/github.com/go-playground/errors)
6+
[![GoDoc](https://godoc.org/github.com/go-playground/errors?status.svg)](https://pkg.go.dev/github.com/go-playground/errors/v5)
77
![License](https://img.shields.io/dub/l/vibe-d.svg)
88

99
Package errors is an errors wrapping package to help propagate and chain errors as well as attach
1010
stack traces, tags(additional information) and even a Type classification system to categorize errors into types eg. Permanent vs Transient.
1111

12+
It is a drop in replacement for the std Go [errors](https://golang.org/pkg/errors/) package.
13+
It is also 100% compatible with the Is, As and Unwrap interfaces used within the std library and can be mixed with the built-in error wrapping.
1214

1315
Common Questions
1416

1517
Why another package?
1618
There are two main reasons.
17-
- I think that the programs generating the original error(s) should be responsible for handling them, even though this package allows access to the original error, and that the callers are mainly interested in:
19+
- I think that the programs generating the original error(s) should be responsible for adding internal metadata and details to them. Ultimately developers are concerned with:
1820
- If the error is Transient or Permanent for retries.
1921
- Additional details for logging.
2022

@@ -83,28 +85,6 @@ func level2(value string) error {
8385
}
8486
```
8587

86-
or using stack only
87-
88-
```go
89-
package main
90-
91-
import (
92-
"fmt"
93-
94-
"github.com/go-playground/errors/v5"
95-
)
96-
97-
func main() {
98-
// maybe you just want to grab a stack trace and process on your own like go-playground/log
99-
// uses it to produce a stack trace log message
100-
frame := errors.Stack()
101-
fmt.Printf("Function: %s File: %s Line: %d\n", frame.Function(), frame.File(), frame.Line())
102-
103-
// and still have access to the underlying runtime.Frame
104-
fmt.Printf("%+v\n", frame.Frame)
105-
}
106-
```
107-
10888
Package Versioning
10989
----------
11090
Using Go modules and proper semantic version releases (as always).

_examples/basic/main.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"io"
66

77
"github.com/go-playground/errors/v5"
8+
nestedpackagee "github.com/go-playground/errors/v5/_examples/basic/nestedpackage"
89
)
910

1011
func main() {
11-
err := level1("testing error")
12+
err := level1Function("1")
1213
fmt.Println(err)
1314
if errors.HasType(err, "Permanent") {
1415
// os.Exit(1)
@@ -32,14 +33,14 @@ func main() {
3233
}
3334
}
3435

35-
func level1(value string) error {
36-
if err := level2(value); err != nil {
36+
func level1Function(userID string) error {
37+
if err := level2Function(userID); err != nil {
3738
return errors.Wrap(err, "level2 call failed")
3839
}
3940
return nil
4041
}
4142

42-
func level2(value string) error {
43-
err := io.EOF
44-
return errors.Wrap(err, "failed to do something").AddTypes("Permanent").AddTags(errors.T("value", value))
43+
func level2Function(userID string) error {
44+
err := nestedpackagee.GetUser(userID)
45+
return errors.Wrap(err, "GetUser something went wrong")
4546
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package nestedpackagee
2+
3+
import (
4+
"io"
5+
6+
"github.com/go-playground/errors/v5"
7+
)
8+
9+
func GetUser(userID string) error {
10+
err := io.EOF
11+
return errors.Wrap(err, "failed to do something").AddTypes("Permanent").AddTags(errors.T("user_id", userID))
12+
}

_examples/stack/main.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

benchmarks_test.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,48 @@ import (
44
"testing"
55
)
66

7-
func BenchmarkError(b *testing.B) {
7+
func BenchmarkErrorNew(b *testing.B) {
8+
for i := 0; i < b.N; i++ {
9+
_ = New("base error")
10+
}
11+
}
12+
13+
func BenchmarkErrorParallelNew(b *testing.B) {
14+
b.RunParallel(func(pb *testing.PB) {
15+
for pb.Next() {
16+
_ = New("base error")
17+
}
18+
})
19+
}
20+
21+
func BenchmarkErrorPrint(b *testing.B) {
822
err := New("base error")
923
for i := 0; i < b.N; i++ {
1024
_ = err.Error()
1125
}
1226
}
1327

14-
func BenchmarkErrorParallel(b *testing.B) {
28+
func BenchmarkErrorParallelPrint(b *testing.B) {
1529
err := New("base error")
1630
b.RunParallel(func(pb *testing.PB) {
1731
for pb.Next() {
1832
_ = err.Error()
1933
}
2034
})
2135
}
36+
37+
func BenchmarkErrorPrintWithTagsAndTypes(b *testing.B) {
38+
err := New("base error").AddTag("key", "value").AddTypes("Permanent", "Other")
39+
for i := 0; i < b.N; i++ {
40+
_ = err.Error()
41+
}
42+
}
43+
44+
func BenchmarkErrorParallelPrintWithTagsAndTypes(b *testing.B) {
45+
err := New("base error").AddTag("key", "value").AddTypes("Permanent", "Other")
46+
b.RunParallel(func(pb *testing.PB) {
47+
for pb.Next() {
48+
_ = err.Error()
49+
}
50+
})
51+
}

0 commit comments

Comments
 (0)