-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherror.go
93 lines (74 loc) · 2.01 KB
/
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package toglacier
import (
"fmt"
"reflect"
"strings"
"github.com/pkg/errors"
)
const (
// ErrorCodeModifyTolerance error when too many files were modified between
// backups. This is an alert for ransomware infection.
ErrorCodeModifyTolerance ErrorCode = "modify-tolerance"
)
// ErrorCode stores the error type that occurred while processing commands from
// toglacier.
type ErrorCode string
// String translate the error code to a human readable text.
func (e ErrorCode) String() string {
switch e {
case ErrorCodeModifyTolerance:
return "too many files modified, aborting for precaution"
}
return "unknown error code"
}
// Error stores error details from a problem occurred while executing high level
// commands from toglacier.
type Error struct {
Paths []string
Code ErrorCode
Err error
}
func newError(paths []string, code ErrorCode, err error) *Error {
return &Error{
Paths: paths,
Code: code,
Err: errors.WithStack(err),
}
}
// Error returns the error in a human readable format.
func (e Error) Error() string {
return e.String()
}
// String translate the error to a human readable text.
func (e Error) String() string {
var paths string
if e.Paths != nil {
paths = fmt.Sprintf("paths [%s], ", strings.Join(e.Paths, ", "))
}
var err string
if e.Err != nil {
err = fmt.Sprintf(". details: %s", e.Err)
}
return fmt.Sprintf("toglacier: %s%s%s", paths, e.Code, err)
}
// ErrorEqual compares two Error objects. This is useful to compare down to the
// low level errors.
func ErrorEqual(first, second error) bool {
if first == nil || second == nil {
return first == second
}
err1, ok1 := errors.Cause(first).(*Error)
err2, ok2 := errors.Cause(second).(*Error)
if !ok1 || !ok2 {
return false
}
if !reflect.DeepEqual(err1.Paths, err2.Paths) || err1.Code != err2.Code {
return false
}
errCause1 := errors.Cause(err1.Err)
errCause2 := errors.Cause(err2.Err)
if errCause1 == nil || errCause2 == nil {
return errCause1 == errCause2
}
return errCause1.Error() == errCause2.Error()
}