Skip to content

Commit d881fa8

Browse files
deps: remove deprecated github.com/pkg/errors (#1125)
Signed-off-by: Matthieu MOREL <[email protected]> Co-authored-by: Sajay Antony <[email protected]>
1 parent 3cffd9d commit d881fa8

File tree

6 files changed

+30
-22
lines changed

6 files changed

+30
-22
lines changed

.golangci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ linters:
55
disable-all: true
66
enable:
77
- dupl
8+
- errorlint
89
- gofmt
910
- goimports
11+
- gomodguard
1012
- gosimple
1113
- govet
1214
- ineffassign
@@ -19,5 +21,12 @@ linters:
1921
linters-settings:
2022
gofmt:
2123
simplify: true
24+
gomodguard:
25+
blocked:
26+
modules:
27+
- github.com/pkg/errors:
28+
recommendations:
29+
- errors
30+
- fmt
2231
dupl:
2332
threshold: 400

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.18
44

55
require (
66
github.com/opencontainers/go-digest v1.0.0
7-
github.com/pkg/errors v0.9.1
87
github.com/russross/blackfriday v1.6.0
98
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415
109
github.com/xeipuuv/gojsonschema v1.2.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
55
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
6-
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
7-
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
86
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
97
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
108
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=

schema/error.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package schema
1717
import (
1818
"bufio"
1919
"encoding/json"
20+
"errors"
2021
"io"
2122
)
2223

@@ -34,7 +35,8 @@ func (e *SyntaxError) Error() string { return e.msg }
3435
// and converts it into a *schema.SyntaxError containing line/col information using the given reader.
3536
// If the given error is not a *json.SyntaxError it is returned unchanged.
3637
func WrapSyntaxError(r io.Reader, err error) error {
37-
if serr, ok := err.(*json.SyntaxError); ok {
38+
var serr *json.SyntaxError
39+
if errors.As(err, &serr) {
3840
buf := bufio.NewReader(r)
3941
line := 0
4042
col := 0

schema/spec_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package schema_test
1616

1717
import (
1818
"bytes"
19+
"errors"
1920
"fmt"
2021
"io"
2122
"net/url"
@@ -26,7 +27,6 @@ import (
2627
"testing"
2728

2829
"github.com/opencontainers/image-spec/schema"
29-
"github.com/pkg/errors"
3030
"github.com/russross/blackfriday"
3131
)
3232

@@ -93,7 +93,7 @@ func validate(t *testing.T, name string) {
9393
}
9494

9595
for _, example := range examples {
96-
if example.Err == errFormatInvalid && example.Mediatype == "" { // ignore
96+
if errors.Is(example.Err, errFormatInvalid) && example.Mediatype == "" { // ignore
9797
continue
9898
}
9999

@@ -111,7 +111,8 @@ func validate(t *testing.T, name string) {
111111
}
112112

113113
var errs []error
114-
if verr, ok := errors.Cause(err).(schema.ValidationError); ok {
114+
var verr schema.ValidationError
115+
if errors.As(err, &verr) {
115116
errs = verr.Errs
116117
} else {
117118
printFields(t, "error", example.Mediatype, example.Title, err)

schema/validator.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ package schema
1717
import (
1818
"bytes"
1919
"encoding/json"
20+
"errors"
2021
"fmt"
2122
"io"
2223
"regexp"
2324

2425
digest "github.com/opencontainers/go-digest"
2526
v1 "github.com/opencontainers/image-spec/specs-go/v1"
26-
"github.com/pkg/errors"
2727
"github.com/xeipuuv/gojsonschema"
2828
)
2929

@@ -53,7 +53,7 @@ func (e ValidationError) Error() string {
5353
func (v Validator) Validate(src io.Reader) error {
5454
buf, err := io.ReadAll(src)
5555
if err != nil {
56-
return errors.Wrap(err, "unable to read the document file")
56+
return fmt.Errorf("unable to read the document file: %w", err)
5757
}
5858

5959
if f, ok := mapValidate[v]; ok {
@@ -71,9 +71,8 @@ func (v Validator) Validate(src io.Reader) error {
7171

7272
result, err := gojsonschema.Validate(sl, ml)
7373
if err != nil {
74-
return errors.Wrapf(
75-
WrapSyntaxError(bytes.NewReader(buf), err),
76-
"schema %s: unable to validate", v)
74+
return fmt.Errorf("schema %s: unable to validate: %w", v,
75+
WrapSyntaxError(bytes.NewReader(buf), err))
7776
}
7877

7978
if result.Valid() {
@@ -101,12 +100,12 @@ func validateManifest(r io.Reader) error {
101100

102101
buf, err := io.ReadAll(r)
103102
if err != nil {
104-
return errors.Wrapf(err, "error reading the io stream")
103+
return fmt.Errorf("error reading the io stream: %w", err)
105104
}
106105

107106
err = json.Unmarshal(buf, &header)
108107
if err != nil {
109-
return errors.Wrap(err, "manifest format mismatch")
108+
return fmt.Errorf("manifest format mismatch: %w", err)
110109
}
111110

112111
if header.Config.MediaType != string(v1.MediaTypeImageConfig) {
@@ -131,16 +130,16 @@ func validateDescriptor(r io.Reader) error {
131130

132131
buf, err := io.ReadAll(r)
133132
if err != nil {
134-
return errors.Wrapf(err, "error reading the io stream")
133+
return fmt.Errorf("error reading the io stream: %w", err)
135134
}
136135

137136
err = json.Unmarshal(buf, &header)
138137
if err != nil {
139-
return errors.Wrap(err, "descriptor format mismatch")
138+
return fmt.Errorf("descriptor format mismatch: %w", err)
140139
}
141140

142141
err = header.Digest.Validate()
143-
if err == digest.ErrDigestUnsupported {
142+
if errors.Is(err, digest.ErrDigestUnsupported) {
144143
// we ignore unsupported algorithms
145144
fmt.Printf("warning: unsupported digest: %q: %v\n", header.Digest, err)
146145
return nil
@@ -153,12 +152,12 @@ func validateIndex(r io.Reader) error {
153152

154153
buf, err := io.ReadAll(r)
155154
if err != nil {
156-
return errors.Wrapf(err, "error reading the io stream")
155+
return fmt.Errorf("error reading the io stream: %w", err)
157156
}
158157

159158
err = json.Unmarshal(buf, &header)
160159
if err != nil {
161-
return errors.Wrap(err, "index format mismatch")
160+
return fmt.Errorf("index format mismatch: %w", err)
162161
}
163162

164163
for _, manifest := range header.Manifests {
@@ -180,12 +179,12 @@ func validateConfig(r io.Reader) error {
180179

181180
buf, err := io.ReadAll(r)
182181
if err != nil {
183-
return errors.Wrapf(err, "error reading the io stream")
182+
return fmt.Errorf("error reading the io stream: %w", err)
184183
}
185184

186185
err = json.Unmarshal(buf, &header)
187186
if err != nil {
188-
return errors.Wrap(err, "config format mismatch")
187+
return fmt.Errorf("config format mismatch: %w", err)
189188
}
190189

191190
checkPlatform(header.OS, header.Architecture)
@@ -194,7 +193,7 @@ func validateConfig(r io.Reader) error {
194193
envRegexp := regexp.MustCompile(`^[^=]+=.*$`)
195194
for _, e := range header.Config.Env {
196195
if !envRegexp.MatchString(e) {
197-
return errors.Errorf("unexpected env: %q", e)
196+
return fmt.Errorf("unexpected env: %q", e)
198197
}
199198
}
200199

0 commit comments

Comments
 (0)