Skip to content

Commit be9c10b

Browse files
author
Dean Karn
authored
Updates (#14)
1 parent 5de7463 commit be9c10b

File tree

9 files changed

+122
-74
lines changed

9 files changed

+122
-74
lines changed

.github/workflows/go.yml

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,45 @@
1-
name: Lint & Test
2-
on: [push, pull_request]
1+
on:
2+
push:
3+
branches:
4+
- master
5+
pull_request:
6+
name: Test
37
jobs:
48
test:
59
strategy:
610
matrix:
7-
go-version: [1.14.x,1.13.x]
8-
platform: [ubuntu-latest, macos-latest, windows-latest]
9-
runs-on: ${{ matrix.platform }}
11+
go-version: [1.18.x]
12+
os: [ubuntu-latest, macos-latest, windows-latest]
13+
runs-on: ${{ matrix.os }}
1014
steps:
1115
- name: Install Go
12-
uses: actions/setup-go@v1
16+
uses: actions/setup-go@v3
1317
with:
1418
go-version: ${{ matrix.go-version }}
1519

16-
- name: Priming Cache
17-
uses: actions/cache@v1
20+
- name: Checkout code
21+
uses: actions/checkout@v3
22+
23+
- name: Restore Cache
24+
uses: actions/cache@v3
1825
with:
1926
path: ~/go/pkg/mod
2027
key: ${{ runner.os }}-v1-go-${{ hashFiles('**/go.sum') }}
2128
restore-keys: |
2229
${{ runner.os }}-v1-go-
2330
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-
3431
- name: Test
35-
run: go test ./...
32+
run: go test -race ./...
33+
34+
golangci:
35+
name: lint
36+
runs-on: ubuntu-latest
37+
steps:
38+
- uses: actions/setup-go@v3
39+
with:
40+
go-version: 1.18.x
41+
- uses: actions/checkout@v3
42+
- name: golangci-lint
43+
uses: golangci/golangci-lint-action@v3
44+
with:
45+
version: v1.46.2

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [Unreleased]
8+
9+
## [5.2.0] - 2022-05-28
10+
### Added
11+
- Wrap check for nil errors which will now panic to indicate a larger issue, the caller NOT checking an error value. This caused a panic when trying to print the error if nil error was wrapped.
12+
13+
### Removed
14+
- Deprecated information in the documentation.
15+
16+
### Changed
17+
- Updated deps.
18+
19+
20+
[Unreleased]: https://github.com/rust-playground/relay-rs/compare/v5.2.0...HEAD
21+
[5.2.0]: https://github.com/rust-playground/relay-rs/compare/v5.1.1...v5.2.0

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ GOCMD=GO111MODULE=on go
33
linters-install:
44
@golangci-lint --version >/dev/null 2>&1 || { \
55
echo "installing linting tools..."; \
6-
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s v1.21.0; \
6+
brew install golangci-lint; \
77
}
88

99
lint: linters-install
10-
$(PWD)/bin/golangci-lint run
10+
@golangci-lint run
1111

1212
test:
1313
$(GOCMD) test -cover -race ./...

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package errors
22
============
3-
![Project status](https://img.shields.io/badge/version-5.1.1-green.svg)
3+
![Project status](https://img.shields.io/badge/version-5.2.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)
66
[![GoDoc](https://godoc.org/github.com/go-playground/errors?status.svg)](https://pkg.go.dev/github.com/go-playground/errors/v5)

chain.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ var (
1717
)
1818

1919
// T is a shortcut to make a Tag
20-
func T(key string, value interface{}) Tag {
20+
func T(key string, value any) Tag {
2121
return Tag{Key: key, Value: value}
2222
}
2323

2424
// Tag contains a single key value combination
2525
// to be attached to your error
2626
type Tag struct {
2727
Key string
28-
Value interface{}
28+
Value any
2929
}
3030

3131
func newLink(err error, prefix string, skipFrames int) *Link {
@@ -42,16 +42,7 @@ type Chain []*Link
4242

4343
// Error returns the formatted error string
4444
func (c Chain) Error() string {
45-
//if errFormatFn != nil {
4645
return errFormatFn(c)
47-
//}
48-
//b := make([]byte, 0, len(c)*192)
49-
//
50-
//for i := 0; i < len(c); i++ {
51-
// b = c[i].formatError(b)
52-
// b = append(b, '\n')
53-
//}
54-
//return unsafeext.BytesToString(b[:len(b)-1])
5546
}
5647

5748
// Link contains a single error entry, unless it's the top level error, in
@@ -186,7 +177,7 @@ func (c Chain) AddTags(tags ...Tag) Chain {
186177
}
187178

188179
// AddTag allows the addition of a single tag
189-
func (c Chain) AddTag(key string, value interface{}) Chain {
180+
func (c Chain) AddTag(key string, value any) Chain {
190181
return c.AddTags(Tag{Key: key, Value: value})
191182
}
192183

@@ -202,7 +193,7 @@ func (c Chain) Wrap(prefix string) Chain {
202193
return wrap(c, prefix, 3)
203194
}
204195

205-
// Unwrap returns the result of calling the Unwrap method on err, if err's
196+
// Unwrap returns the result of calling the Unwrap method on an error, if the errors
206197
// type contains an Unwrap method returning error.
207198
// Otherwise, Unwrap returns nil.
208199
//
@@ -217,31 +208,28 @@ func (c Chain) Unwrap() error {
217208
return c[:len(c)-1]
218209
}
219210

220-
// Is reports whether any error in err's chain matches target.
221-
//
222-
// This is here to help make it a drop in replacement to the std error handler, I highly recommend using
223-
// Cause + switch statement instead.
211+
// Is reports whether any error in error chain matches target.
224212
func (c Chain) Is(target error) bool {
225213
return stderrors.Is(c[len(c)-1].Err, target)
226214
}
227215

228-
// As finds the first error in err's chain that matches target, and if so, sets
216+
// As finds the first error in the error chain that matches target, and if so, sets
229217
// target to that error value and returns true. Otherwise, it returns false.
230218
//
231219
// The chain consists of err itself followed by the sequence of errors obtained by
232220
// repeatedly calling Unwrap.
233221
//
234222
// An error matches target if the error's concrete value is assignable to the value
235-
// pointed to by target, or if the error has a method As(interface{}) bool such that
223+
// pointed to by target, or if the error has a method As(any) bool such that
236224
// As(target) returns true. In the latter case, the As method is responsible for
237225
// setting target.
238226
//
239-
// An error type might provide an As method so it can be treated as if it were a
240-
// a different error type.
227+
// An error type might provide an As method, so it can be treated as if it were a
228+
// different error type.
241229
//
242230
// As panics if target is not a non-nil pointer to either a type that implements
243231
// error, or to any interface type.
244-
func (c Chain) As(target interface{}) bool {
232+
func (c Chain) As(target any) bool {
245233
return stderrors.As(c[len(c)-1].Err, target)
246234
}
247235

errors.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
type unwrap interface{ Unwrap() error }
1111
type is interface{ Is(error) bool }
12-
type as interface{ As(interface{}) bool }
12+
type as interface{ As(any) bool }
1313

1414
// ErrorFormatFn represents the error formatting function for a Chain of errors.
1515
type ErrorFormatFn func(Chain) string
@@ -42,8 +42,8 @@ func New(s string) Chain {
4242
}
4343

4444
// Newf creates an error with the provided text and automatically wraps it with line information.
45-
// it also accepts a varadic for optional message formatting.
46-
func Newf(format string, a ...interface{}) Chain {
45+
// it also accepts a variadic for optional message formatting.
46+
func Newf(format string, a ...any) Chain {
4747
return wrap(fmt.Errorf(format, a...), "", 3)
4848
}
4949

@@ -55,8 +55,8 @@ func Wrap(err error, prefix string) Chain {
5555

5656
// Wrapf encapsulates the error, stores a contextual prefix and automatically obtains
5757
// a stack trace.
58-
// it also accepts a varadic for prefix formatting.
59-
func Wrapf(err error, prefix string, a ...interface{}) Chain {
58+
// it also accepts a variadic for prefix formatting.
59+
func Wrapf(err error, prefix string, a ...any) Chain {
6060
return wrap(err, fmt.Sprintf(prefix, a...), 3)
6161
}
6262

@@ -67,6 +67,9 @@ func WrapSkipFrames(err error, prefix string, n uint) Chain {
6767
}
6868

6969
func wrap(err error, prefix string, skipFrames int) (c Chain) {
70+
if err == nil {
71+
panic("errors: Wrap|Wrapf called with nil error")
72+
}
7073
var ok bool
7174
if c, ok = err.(Chain); ok {
7275
c = append(c, newLink(err, prefix, skipFrames))
@@ -81,7 +84,7 @@ func wrap(err error, prefix string, skipFrames int) (c Chain) {
8184
return
8285
}
8386

84-
// Cause extracts and returns the root wrapped error (the naked error with no additional information
87+
// Cause extracts and returns the root wrapped error (the naked error with no additional information)
8588
func Cause(err error) error {
8689
for {
8790
switch t := err.(type) {
@@ -121,8 +124,8 @@ func HasType(err error, typ string) bool {
121124
}
122125
}
123126

124-
// LookupTag recursively searches for the provided tag and returns it's value or nil
125-
func LookupTag(err error, key string) interface{} {
127+
// LookupTag recursively searches for the provided tag and returns its value or nil
128+
func LookupTag(err error, key string) any {
126129
for {
127130
switch t := err.(type) {
128131
case Chain:
@@ -143,17 +146,17 @@ func LookupTag(err error, key string) interface{} {
143146
}
144147
}
145148

146-
// Is is to allow this library to be a drop-in replacement to the std library.
149+
// Is allows this library to be a drop-in replacement to the std library.
147150
//
148-
// Is reports whether any error in err's chain matches target.
151+
// Is reports whether any error in the error chain matches target.
149152
//
150153
// The chain consists of err itself followed by the sequence of errors obtained by
151154
// repeatedly calling Unwrap.
152155
//
153156
// An error is considered to match a target if it is equal to that target or if
154157
// it implements a method Is(error) bool such that Is(target) returns true.
155158
//
156-
// An error type might provide an Is method so it can be treated as equivalent
159+
// An error type might provide an Is method, so it can be treated as equivalent
157160
// to an existing error. For example, if MyError defines
158161
//
159162
// func (m MyError) Is(target error) bool { return target == os.ErrExist }
@@ -166,22 +169,22 @@ func Is(err, target error) bool {
166169

167170
// As is to allow this library to be a drop-in replacement to the std library.
168171
//
169-
// As finds the first error in err's chain that matches target, and if so, sets
172+
// As finds the first error in the error chain that matches target, and if so, sets
170173
// target to that error value and returns true. Otherwise, it returns false.
171174
//
172175
// The chain consists of err itself followed by the sequence of errors obtained by
173176
// repeatedly calling Unwrap.
174177
//
175178
// An error matches target if the error's concrete value is assignable to the value
176-
// pointed to by target, or if the error has a method As(interface{}) bool such that
179+
// pointed to by target, or if the error has a method As(any) bool such that
177180
// As(target) returns true. In the latter case, the As method is responsible for
178181
// setting target.
179182
//
180-
// An error type might provide an As method so it can be treated as if it were a
183+
// An error type might provide an As method, so it can be treated as if it were
181184
// a different error type.
182185
//
183186
// As panics if target is not a non-nil pointer to either a type that implements
184187
// error, or to any interface type.
185-
func As(err error, target interface{}) bool {
186-
return stderrors.As(err, target)
188+
func As(err error, target any) bool {
189+
return stderrors.As(err, &target)
187190
}

errors_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,15 @@ func TestWrap(t *testing.T) {
7070
func TestUnwrap(t *testing.T) {
7171
defaultErr := stderrors.New("this is an error")
7272
err := fmt.Errorf("std wrapped: %w", defaultErr)
73+
assertError(t, err)
7374
err = Wrap(defaultErr, "prefix")
75+
assertError(t, err)
7476
err = Wrap(err, "prefix2")
77+
assertError(t, err)
7578
err = fmt.Errorf("wrapping Chain: %w", err)
79+
assertError(t, err)
7680
err = fmt.Errorf("wrapping err again: %w", err)
81+
assertError(t, err)
7782
err = Wrap(err, "wrapping std with chain")
7883

7984
for {
@@ -92,6 +97,12 @@ func TestUnwrap(t *testing.T) {
9297
}
9398
}
9499

100+
func assertError(t *testing.T, err error) {
101+
if err == nil {
102+
t.Fatal("err is nil")
103+
}
104+
}
105+
95106
func TestTags(t *testing.T) {
96107
tests := []struct {
97108
err string
@@ -195,10 +206,15 @@ func TestCause2(t *testing.T) {
195206
func TestCauseStdErrorsMixed(t *testing.T) {
196207
defaultErr := stderrors.New("this is an error")
197208
err := fmt.Errorf("std wrapped: %w", defaultErr)
209+
assertError(t, err)
198210
err = Wrap(defaultErr, "prefix")
211+
assertError(t, err)
199212
err = Wrap(err, "prefix2")
213+
assertError(t, err)
200214
err = fmt.Errorf("wrapping Chain: %w", err)
215+
assertError(t, err)
201216
err = fmt.Errorf("wrapping err again: %w", err)
217+
assertError(t, err)
202218
err = Wrap(err, "wrapping std with chain")
203219
cause := Cause(err)
204220
expect := defaultErr.Error()
@@ -228,7 +244,7 @@ func TestLookupTag(t *testing.T) {
228244
name string
229245
err error
230246
key string
231-
value interface{}
247+
value any
232248
}{
233249
{
234250
name: "basic wrap",
@@ -257,10 +273,15 @@ func TestLookupTag(t *testing.T) {
257273
func TestIs(t *testing.T) {
258274
defaultErr := io.EOF
259275
err := fmt.Errorf("std wrapped: %w", defaultErr)
276+
assertError(t, err)
260277
err = Wrap(defaultErr, "prefix")
278+
assertError(t, err)
261279
err = Wrap(err, "prefix2")
280+
assertError(t, err)
262281
err = fmt.Errorf("wrapping Chain: %w", err)
282+
assertError(t, err)
263283
err = fmt.Errorf("wrapping err again: %w", err)
284+
assertError(t, err)
264285
err = Wrap(err, "wrapping std with chain")
265286
if !Is(err, io.EOF) {
266287
t.Fatal("want true got false")
@@ -278,10 +299,15 @@ func (e *myErrorType) Error() string {
278299
func TestAs(t *testing.T) {
279300
defaultErr := &myErrorType{msg: "my error type"}
280301
err := fmt.Errorf("std wrapped: %w", defaultErr)
302+
assertError(t, err)
281303
err = Wrap(defaultErr, "prefix")
304+
assertError(t, err)
282305
err = Wrap(err, "prefix2")
306+
assertError(t, err)
283307
err = fmt.Errorf("wrapping Chain: %w", err)
308+
assertError(t, err)
284309
err = fmt.Errorf("wrapping err again: %w", err)
310+
assertError(t, err)
285311
err = Wrap(err, "wrapping std with chain")
286312

287313
var myErr *myErrorType

0 commit comments

Comments
 (0)