forked from peak/s5cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
89 lines (78 loc) · 1.79 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
package command
import (
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
errorpkg "github.com/peak/s5cmd/error"
"github.com/peak/s5cmd/log"
"github.com/peak/s5cmd/storage/url"
)
func printDebug(op string, src, dst *url.URL, err error) {
msg := log.DebugMessage{
Command: fmt.Sprintf("%v %v %v", op, src, dst),
Operation: op,
Err: cleanupError(err),
}
log.Debug(msg)
}
// printError is the helper function to log error messages.
func printError(command, op string, err error) {
// dont print cancelation errors
if errorpkg.IsCancelation(err) {
return
}
// check if we have our own error type
{
cerr, ok := err.(*errorpkg.Error)
if ok {
msg := log.ErrorMessage{
Err: cleanupError(cerr.Err),
Command: cerr.FullCommand(),
Operation: cerr.Op,
}
log.Error(msg)
return
}
}
// check if errors are aggregated
{
merr, ok := err.(*multierror.Error)
if ok {
for _, err := range merr.Errors {
customErr, ok := err.(*errorpkg.Error)
if ok {
msg := log.ErrorMessage{
Err: cleanupError(customErr.Err),
Command: customErr.FullCommand(),
Operation: customErr.Op,
}
log.Error(msg)
continue
}
msg := log.ErrorMessage{
Err: cleanupError(err),
Command: command,
Operation: op,
}
log.Error(msg)
}
return
}
}
// we don't know the exact error type. log the error as is.
msg := log.ErrorMessage{
Err: cleanupError(err),
Command: command,
Operation: op,
}
log.Error(msg)
}
// cleanupError converts multiline messages into
// a single line.
func cleanupError(err error) string {
s := strings.Replace(err.Error(), "\n", " ", -1)
s = strings.Replace(s, "\t", " ", -1)
s = strings.Replace(s, " ", " ", -1)
s = strings.TrimSpace(s)
return s
}