Skip to content

Commit 3a2b819

Browse files
authored
Refactor errors to use pointer receivers (#602)
Signed-off-by: Cody Soyland <codysoyland@github.com>
1 parent a1a9c88 commit 3a2b819

File tree

11 files changed

+271
-201
lines changed

11 files changed

+271
-201
lines changed

metadata/errors.go

Lines changed: 96 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,78 +34,116 @@ type ErrRepository struct {
3434
Msg string
3535
}
3636

37-
func (e ErrRepository) Error() string {
37+
func (e *ErrRepository) Error() string {
3838
return fmt.Sprintf("repository error: %s", e.Msg)
3939
}
4040

41+
func (e *ErrRepository) Is(target error) bool {
42+
_, ok := target.(*ErrRepository)
43+
return ok
44+
}
45+
4146
// ErrUnsignedMetadata - An error about metadata object with insufficient threshold of signatures
4247
type ErrUnsignedMetadata struct {
4348
Msg string
4449
}
4550

46-
func (e ErrUnsignedMetadata) Error() string {
51+
func (e *ErrUnsignedMetadata) Error() string {
4752
return fmt.Sprintf("unsigned metadata error: %s", e.Msg)
4853
}
4954

5055
// ErrUnsignedMetadata is a subset of ErrRepository
51-
func (e ErrUnsignedMetadata) Is(target error) bool {
52-
return target == ErrRepository{} || target == ErrUnsignedMetadata{}
56+
func (e *ErrUnsignedMetadata) Is(target error) bool {
57+
if _, ok := target.(*ErrUnsignedMetadata); ok {
58+
return true
59+
}
60+
if _, ok := target.(*ErrRepository); ok {
61+
return true
62+
}
63+
return false
5364
}
5465

5566
// ErrBadVersionNumber - An error for metadata that contains an invalid version number
5667
type ErrBadVersionNumber struct {
5768
Msg string
5869
}
5970

60-
func (e ErrBadVersionNumber) Error() string {
71+
func (e *ErrBadVersionNumber) Error() string {
6172
return fmt.Sprintf("bad version number error: %s", e.Msg)
6273
}
6374

6475
// ErrBadVersionNumber is a subset of ErrRepository
65-
func (e ErrBadVersionNumber) Is(target error) bool {
66-
return target == ErrRepository{} || target == ErrBadVersionNumber{}
76+
func (e *ErrBadVersionNumber) Is(target error) bool {
77+
if _, ok := target.(*ErrBadVersionNumber); ok {
78+
return true
79+
}
80+
if _, ok := target.(*ErrRepository); ok {
81+
return true
82+
}
83+
return false
6784
}
6885

6986
// ErrEqualVersionNumber - An error for metadata containing a previously verified version number
7087
type ErrEqualVersionNumber struct {
7188
Msg string
7289
}
7390

74-
func (e ErrEqualVersionNumber) Error() string {
91+
func (e *ErrEqualVersionNumber) Error() string {
7592
return fmt.Sprintf("equal version number error: %s", e.Msg)
7693
}
7794

7895
// ErrEqualVersionNumber is a subset of both ErrRepository and ErrBadVersionNumber
79-
func (e ErrEqualVersionNumber) Is(target error) bool {
80-
return target == ErrRepository{} || target == ErrBadVersionNumber{} || target == ErrEqualVersionNumber{}
96+
func (e *ErrEqualVersionNumber) Is(target error) bool {
97+
if _, ok := target.(*ErrEqualVersionNumber); ok {
98+
return true
99+
}
100+
if _, ok := target.(*ErrBadVersionNumber); ok {
101+
return true
102+
}
103+
if _, ok := target.(*ErrRepository); ok {
104+
return true
105+
}
106+
return false
81107
}
82108

83109
// ErrExpiredMetadata - Indicate that a TUF Metadata file has expired
84110
type ErrExpiredMetadata struct {
85111
Msg string
86112
}
87113

88-
func (e ErrExpiredMetadata) Error() string {
114+
func (e *ErrExpiredMetadata) Error() string {
89115
return fmt.Sprintf("expired metadata error: %s", e.Msg)
90116
}
91117

92118
// ErrExpiredMetadata is a subset of ErrRepository
93-
func (e ErrExpiredMetadata) Is(target error) bool {
94-
return target == ErrRepository{} || target == ErrExpiredMetadata{}
119+
func (e *ErrExpiredMetadata) Is(target error) bool {
120+
if _, ok := target.(*ErrExpiredMetadata); ok {
121+
return true
122+
}
123+
if _, ok := target.(*ErrRepository); ok {
124+
return true
125+
}
126+
return false
95127
}
96128

97129
// ErrLengthOrHashMismatch - An error while checking the length and hash values of an object
98130
type ErrLengthOrHashMismatch struct {
99131
Msg string
100132
}
101133

102-
func (e ErrLengthOrHashMismatch) Error() string {
134+
func (e *ErrLengthOrHashMismatch) Error() string {
103135
return fmt.Sprintf("length/hash verification error: %s", e.Msg)
104136
}
105137

106138
// ErrLengthOrHashMismatch is a subset of ErrRepository
107-
func (e ErrLengthOrHashMismatch) Is(target error) bool {
108-
return target == ErrRepository{} || target == ErrLengthOrHashMismatch{}
139+
func (e *ErrLengthOrHashMismatch) Is(target error) bool {
140+
if _, ok := target.(*ErrLengthOrHashMismatch); ok {
141+
return true
142+
}
143+
if _, ok := target.(*ErrRepository); ok {
144+
return true
145+
}
146+
return false
109147
}
110148

111149
// Download errors
@@ -115,22 +153,33 @@ type ErrDownload struct {
115153
Msg string
116154
}
117155

118-
func (e ErrDownload) Error() string {
156+
func (e *ErrDownload) Error() string {
119157
return fmt.Sprintf("download error: %s", e.Msg)
120158
}
121159

160+
func (e *ErrDownload) Is(target error) bool {
161+
_, ok := target.(*ErrDownload)
162+
return ok
163+
}
164+
122165
// ErrDownloadLengthMismatch - Indicate that a mismatch of lengths was seen while downloading a file
123166
type ErrDownloadLengthMismatch struct {
124167
Msg string
125168
}
126169

127-
func (e ErrDownloadLengthMismatch) Error() string {
170+
func (e *ErrDownloadLengthMismatch) Error() string {
128171
return fmt.Sprintf("download length mismatch error: %s", e.Msg)
129172
}
130173

131174
// ErrDownloadLengthMismatch is a subset of ErrDownload
132-
func (e ErrDownloadLengthMismatch) Is(target error) bool {
133-
return target == ErrDownload{} || target == ErrDownloadLengthMismatch{}
175+
func (e *ErrDownloadLengthMismatch) Is(target error) bool {
176+
if _, ok := target.(*ErrDownloadLengthMismatch); ok {
177+
return true
178+
}
179+
if _, ok := target.(*ErrDownload); ok {
180+
return true
181+
}
182+
return false
134183
}
135184

136185
// ErrDownloadHTTP - Returned by Fetcher interface implementations for HTTP errors
@@ -139,38 +188,59 @@ type ErrDownloadHTTP struct {
139188
URL string
140189
}
141190

142-
func (e ErrDownloadHTTP) Error() string {
191+
func (e *ErrDownloadHTTP) Error() string {
143192
return fmt.Sprintf("failed to download %s, http status code: %d", e.URL, e.StatusCode)
144193
}
145194

146195
// ErrDownloadHTTP is a subset of ErrDownload
147-
func (e ErrDownloadHTTP) Is(target error) bool {
148-
return target == ErrDownload{} || target == ErrDownloadHTTP{}
196+
func (e *ErrDownloadHTTP) Is(target error) bool {
197+
if _, ok := target.(*ErrDownloadHTTP); ok {
198+
return true
199+
}
200+
if _, ok := target.(*ErrDownload); ok {
201+
return true
202+
}
203+
return false
149204
}
150205

151206
// ValueError
152207
type ErrValue struct {
153208
Msg string
154209
}
155210

156-
func (e ErrValue) Error() string {
211+
func (e *ErrValue) Error() string {
157212
return fmt.Sprintf("value error: %s", e.Msg)
158213
}
159214

215+
func (e *ErrValue) Is(err error) bool {
216+
_, ok := err.(*ErrValue)
217+
return ok
218+
}
219+
160220
// TypeError
161221
type ErrType struct {
162222
Msg string
163223
}
164224

165-
func (e ErrType) Error() string {
225+
func (e *ErrType) Error() string {
166226
return fmt.Sprintf("type error: %s", e.Msg)
167227
}
168228

229+
func (e *ErrType) Is(err error) bool {
230+
_, ok := err.(*ErrType)
231+
return ok
232+
}
233+
169234
// RuntimeError
170235
type ErrRuntime struct {
171236
Msg string
172237
}
173238

174-
func (e ErrRuntime) Error() string {
239+
func (e *ErrRuntime) Error() string {
175240
return fmt.Sprintf("runtime error: %s", e.Msg)
176241
}
242+
243+
func (e *ErrRuntime) Is(err error) bool {
244+
_, ok := err.(*ErrRuntime)
245+
return ok
246+
}

metadata/fetcher/fetcher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (d *DefaultFetcher) DownloadFile(urlPath string, maxLength int64, timeout t
5757
defer res.Body.Close()
5858
// Handle HTTP status codes.
5959
if res.StatusCode == http.StatusNotFound || res.StatusCode == http.StatusForbidden || res.StatusCode != http.StatusOK {
60-
return nil, metadata.ErrDownloadHTTP{StatusCode: res.StatusCode, URL: urlPath}
60+
return nil, &metadata.ErrDownloadHTTP{StatusCode: res.StatusCode, URL: urlPath}
6161
}
6262
var length int64
6363
// Get content length from header (might not be accurate, -1 or not set).
@@ -68,7 +68,7 @@ func (d *DefaultFetcher) DownloadFile(urlPath string, maxLength int64, timeout t
6868
}
6969
// Error if the reported size is greater than what is expected.
7070
if length > maxLength {
71-
return nil, metadata.ErrDownloadLengthMismatch{Msg: fmt.Sprintf("download failed for %s, length %d is larger than expected %d", urlPath, length, maxLength)}
71+
return nil, &metadata.ErrDownloadLengthMismatch{Msg: fmt.Sprintf("download failed for %s, length %d is larger than expected %d", urlPath, length, maxLength)}
7272
}
7373
}
7474
// Although the size has been checked above, use a LimitReader in case
@@ -82,7 +82,7 @@ func (d *DefaultFetcher) DownloadFile(urlPath string, maxLength int64, timeout t
8282
// Error if the reported size is greater than what is expected.
8383
length = int64(len(data))
8484
if length > maxLength {
85-
return nil, metadata.ErrDownloadLengthMismatch{Msg: fmt.Sprintf("download failed for %s, length %d is larger than expected %d", urlPath, length, maxLength)}
85+
return nil, &metadata.ErrDownloadLengthMismatch{Msg: fmt.Sprintf("download failed for %s, length %d is larger than expected %d", urlPath, length, maxLength)}
8686
}
8787

8888
return data, nil

metadata/fetcher/fetcher_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ func TestDownLoadFile(t *testing.T) {
6464
desc: "Path does not exist",
6565
url: "https://jku.github.io/tuf-demo/metadata/badPath.json",
6666
data: nil,
67-
wantErr: metadata.ErrDownloadHTTP{},
67+
wantErr: &metadata.ErrDownloadHTTP{},
6868
},
6969
{
7070
name: "data too long",
7171
desc: "Returned data is longer than maxLength",
7272
url: "https://jku.github.io/tuf-demo/metadata/1.root.json",
7373
maxLength: 1,
7474
data: nil,
75-
wantErr: metadata.ErrDownloadLengthMismatch{},
75+
wantErr: &metadata.ErrDownloadLengthMismatch{},
7676
},
7777
} {
7878
t.Run(tt.name, func(t *testing.T) {

metadata/marshal.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func (meta *Metadata[T]) UnmarshalJSON(data []byte) error {
335335
meta.Signed = i.(T)
336336
meta.Signatures = dict.Signatures
337337
default:
338-
return ErrValue{Msg: "unrecognized metadata type"}
338+
return &ErrValue{Msg: "unrecognized metadata type"}
339339
}
340340
delete(m, "signed")
341341
delete(m, "signatures")
@@ -470,7 +470,7 @@ func (role DelegatedRole) MarshalJSON() ([]byte, error) {
470470
dict["terminating"] = role.Terminating
471471
// make sure we have only one of the two (per spec)
472472
if role.Paths != nil && role.PathHashPrefixes != nil {
473-
return nil, ErrValue{Msg: "failed to marshal: not allowed to have both \"paths\" and \"path_hash_prefixes\" present"}
473+
return nil, &ErrValue{Msg: "failed to marshal: not allowed to have both \"paths\" and \"path_hash_prefixes\" present"}
474474
}
475475
if role.Paths != nil {
476476
dict["paths"] = role.Paths

0 commit comments

Comments
 (0)