This repository has been archived by the owner on Sep 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_error.go
73 lines (66 loc) · 1.74 KB
/
file_error.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
68
69
70
71
72
73
package pecoff
import (
"bytes"
"fmt"
)
// FileError represents an internal error which may occur while
// reading/parsing a PE/COFF file.
type FileError struct {
error
innerError error
}
// (Error must be already implemented in FileError by embedded `error`)
// func (e *FileError) Error() string {
// return e.error.Error()
// }
// ToMultiError returns a flattened list of errors
// formed from 'recursive' innerErrors
func (e *FileError) ToMultiError() (list MultiError) {
err := e
for err != nil {
list = append(list, err.error)
if innerError, ok := err.innerError.(*FileError); ok {
err = innerError
} else {
if err.innerError != nil {
list = append(list, err.innerError)
}
break
}
}
return
}
// ErrorFlatten returns err.ToMultiError if `err` implements FileError,
// if not, an `err` without modifications is returned.
func ErrorFlatten(err error) error {
if fe, ok := err.(*FileError); ok {
return fe.ToMultiError()
}
return err
}
// MultiError represents a collection of errors
type MultiError []error
// Error checks count of errors which are stored in the MultiError and
// returns a string represntation of an error, if:
// 0 entries : "" (empty string);
// 1 entry : Error() value of the first error;
// >=2 entries : Error() values separated with new line character ('\n').
func (e MultiError) Error() string {
switch len(e) {
case 0:
return ""
case 1:
return e[0].Error()
default:
var buf bytes.Buffer
// no error checking of Write* methods,
// as according to the docs, err is always nil.
buf.WriteString(fmt.Sprintf("pecoff: multi error (%d)\n", len(e)))
for _, err := range e {
buf.WriteRune('\t')
buf.WriteString(err.Error())
buf.WriteRune('\n')
}
return buf.String()
}
}