/
aserror.go
67 lines (58 loc) · 1.59 KB
/
aserror.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package perr
import (
"fmt"
"encr.dev/pkg/errinsrc"
)
// ListAsErr is a wrapper around a List that implements the error interface
// allowing us to return a List as an error from functions that parse or compile
// an application.
//
// We've not implemented Error on List directly because we want to avoid accidentally
// returning a List as an error, and want to be explicit about it.
type ListAsErr struct {
prefix string
list *List
}
var (
_ error = (*ListAsErr)(nil)
_ errinsrc.ErrorList = (*ListAsErr)(nil) // We implement this to maintain compatibility with errinsrc detection within the Encore Platform
)
// Error returns the list of errors formatted as a single string.
func (r *ListAsErr) Error() string {
if r.prefix != "" {
return fmt.Sprintf("%s: %s", r.prefix, r.list.FormatErrors())
}
return r.list.FormatErrors()
}
// Unwrap returns the list of errors that make up this error.
//
// Note: This version of Unwrap is a Go 1.20+ feature
//
//goland:noinspection GoStandardMethods
func (r *ListAsErr) Unwrap() []error {
rtn := make([]error, len(r.list.errs))
for i, err := range r.list.errs {
rtn[i] = err
}
return rtn
}
// As implements the As method of the error interface.
//
// It supports the following types:
// - **ListAsErr
// - **List
func (r *ListAsErr) As(err any) bool {
switch err := err.(type) {
case **ListAsErr:
*err = r
case **List:
*err = r.list
default:
return false
}
return true
}
// ErrorList returns the list of errors in the source that make up this error.
func (r *ListAsErr) ErrorList() []*errinsrc.ErrInSrc {
return r.list.errs
}