-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
166 lines (142 loc) · 4.14 KB
/
errors.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package backup
import (
"fmt"
"strconv"
"strings"
"github.com/0x07cf-dev/go-backup/internal/lang"
"github.com/0x07cf-dev/go-backup/internal/logger"
)
type BackupErrorCode int8
type BackupError struct {
Code BackupErrorCode
Source string
Message string
}
const (
GenericError BackupErrorCode = iota
CmdInvalid
CmdFailed
PathError
UploadError
DownloadError
)
var backupErrIDs = []string{
"ErrorGeneric",
"ErrorCmdInvalid",
"ErrorCmdFailed",
"ErrorPath",
"ErrorUpload",
"ErrorDownload",
}
func (e BackupErrorCode) ID() string {
return backupErrIDs[e]
}
func (e BackupErrorCode) Error(source string, message string) BackupError {
return BackupError{
Code: e,
Source: source,
Message: message,
}
}
func (e BackupError) Localize(langs ...string) string {
template := map[string]string{
"Source": e.Source,
"Message": e.Message,
}
return lang.GetTranslator().LocalizeTemplate(e.Code.ID(), template, langs...)
}
func getStatus(errCh chan BackupError, preErrCh chan BackupError, postErrCh chan BackupError, langs ...string) (string, string) {
var status strings.Builder
statusEmoji := "green_circle"
// Check all errors
var transferErrors []BackupError
var preErrors []BackupError
var postErrors []BackupError
for err := range errCh {
transferErrors = append(transferErrors, err)
}
for err := range preErrCh {
preErrors = append(preErrors, err)
}
for err := range postErrCh {
postErrors = append(postErrors, err)
}
failedTransfers := len(transferErrors)
failedPre := len(preErrors)
failedPost := len(postErrors)
if failedTransfers == 0 && failedPre == 0 && failedPost == 0 {
status.WriteString(lang.GetTranslator().Localize("Success", langs...))
} else {
possibleFails := cap(errCh) + cap(preErrCh) + cap(postErrCh)
totalFails := failedTransfers + failedPre + failedPost
totalFailRate := int(float32(totalFails) / float32(possibleFails) * 100)
logger.Debugf("Failures: %d/%d (%d%%)", totalFails, possibleFails, totalFailRate)
// Not a perfect run
if totalFailRate > 0 {
str := lang.GetTranslator().Localize("Fail", langs...)
status.WriteString(str + "\n")
logger.Debug(str)
}
if totalFailRate > 98 {
return status.String(), "black_circle"
}
// Append upload errors
templ := "%d° | %s\n"
if failedTransfers > 0 {
transferFailRate := int8(float32(failedTransfers) / float32(cap(errCh)) * 100)
logger.Debugf("Transfers failed: %d/%d (%d%%)\n", failedTransfers, cap(errCh), transferFailRate)
if transferFailRate > 10 {
str := lang.GetTranslator().LocalizeTemplate("FailedTransferNum", map[string]string{
"Failed": fmt.Sprintf("%d%%", transferFailRate),
}, langs...)
status.WriteString(str + "\n")
logger.Debug(str)
}
for i, err := range transferErrors {
str := fmt.Sprintf(templ, i+1, err.Localize(langs...))
status.WriteString(str)
logger.Debug(str)
}
}
// Append pre errors
if failedPre > 0 {
logger.Debugf("Pre-transfer commands failed: %d/%d", failedPre, cap(preErrCh))
str := lang.GetTranslator().LocalizeTemplate("FailedPreNum", map[string]string{
"Failed": strconv.Itoa(failedPre),
}, langs...)
status.WriteString(str + "\n")
logger.Debug(str)
for i, err := range preErrors {
s := fmt.Sprintf(templ, i+1, err.Localize(langs...))
status.WriteString(s)
logger.Debug(s)
}
}
// Append post errors
if failedPost > 0 {
logger.Debugf("Post-transfer commands failed: %d/%d", failedPost, cap(postErrCh))
str := lang.GetTranslator().LocalizeTemplate("FailedPostNum", map[string]string{
"Failed": strconv.Itoa(failedPost),
}, langs...)
status.WriteString(str + "\n")
logger.Debug(str)
for i, err := range postErrors {
s := fmt.Sprintf(templ, i+1, err.Localize(langs...))
status.WriteString(s)
logger.Debug(s)
}
}
// Determine notification circle emoji color based on total fail rate
colors := []string{"green_circle", "yellow_circle", "orange_circle", "red_circle"}
thresh := []int{10, 50, 80, 100}
lev := 0
for i, th := range thresh {
if totalFailRate <= th {
lev = i
break
}
}
statusEmoji = colors[lev%len(colors)]
}
return status.String(), statusEmoji
}