Skip to content

Commit 70fd2d7

Browse files
committed
Add collapsible error type
Signed-off-by: Derek McGowan <[email protected]>
1 parent 6022faf commit 70fd2d7

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

internal/types/collapsible.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package types
18+
19+
import "fmt"
20+
21+
// CollapsibleError indicates the error should be collapsed
22+
type CollapsibleError interface {
23+
CollapseError()
24+
}
25+
26+
// CollapsedError returns a new error with the collapsed
27+
// error returned on unwrapped or when formatted with "%+v"
28+
func CollapsedError(err error, collapsed ...error) error {
29+
return collapsedError{err, collapsed}
30+
}
31+
32+
type collapsedError struct {
33+
error
34+
collapsed []error
35+
}
36+
37+
func (c collapsedError) Unwrap() []error {
38+
return append([]error{c.error}, c.collapsed...)
39+
}
40+
41+
func (c collapsedError) Format(s fmt.State, verb rune) {
42+
switch verb {
43+
case 'v':
44+
if s.Flag('+') {
45+
fmt.Fprintf(s, "%+v", c.error)
46+
for _, err := range c.collapsed {
47+
fmt.Fprintf(s, "\n%+v", err)
48+
}
49+
return
50+
}
51+
fallthrough
52+
case 's':
53+
fmt.Fprint(s, c.Error())
54+
case 'q':
55+
fmt.Fprintf(s, "%q", c.Error())
56+
}
57+
}

0 commit comments

Comments
 (0)