feat(errorHandling): allow error to be fatal

This commit is contained in:
Yukine 2021-02-04 02:45:11 +01:00
parent cfb5636dfd
commit 0de1fd9277
No known key found for this signature in database
GPG Key ID: 6AE0B5764F0126D1
1 changed files with 15 additions and 9 deletions

View File

@ -88,7 +88,7 @@ func main() {
for _, volumeID := range volumeIDs { for _, volumeID := range volumeIDs {
volume, _, err := ctx.DoContext.GetVolume(volumeID) volume, _, err := ctx.DoContext.GetVolume(volumeID)
if err != nil { if err != nil {
errorHandling(err, ctx) errorHandling(ctx, err, true)
return return
} }
@ -97,7 +97,7 @@ func main() {
Name: fmt.Sprintf("%s-%s", volume.Name, time.Now().Format("2006-01-02 15:04:05")), Name: fmt.Sprintf("%s-%s", volume.Name, time.Now().Format("2006-01-02 15:04:05")),
}) })
if err != nil { if err != nil {
errorHandling(err, ctx) errorHandling(ctx, err, true)
return return
} }
@ -109,12 +109,12 @@ func main() {
sort.SliceStable(snapshots, func(firstIndex, secondIndex int) bool { sort.SliceStable(snapshots, func(firstIndex, secondIndex int) bool {
firstTime, err := time.Parse(snapshots[firstIndex].Created, createdAtFormat) firstTime, err := time.Parse(snapshots[firstIndex].Created, createdAtFormat)
if err != nil { if err != nil {
errorHandling(err, ctx) errorHandling(ctx, err, true)
} }
secondTime, err := time.Parse(snapshots[firstIndex].Created, createdAtFormat) secondTime, err := time.Parse(snapshots[firstIndex].Created, createdAtFormat)
if err != nil { if err != nil {
errorHandling(err, ctx) errorHandling(ctx, err, true)
} }
return firstTime.Before(secondTime) return firstTime.Before(secondTime)
@ -126,7 +126,7 @@ func main() {
_, err := ctx.DoContext.DeleteSnapshot(snapshots[i].ID) _, err := ctx.DoContext.DeleteSnapshot(snapshots[i].ID)
if err != nil { if err != nil {
errorHandling(err, ctx) errorHandling(ctx, err, false)
return return
} }
} }
@ -134,13 +134,19 @@ func main() {
} }
} }
func errorHandling(err error, ctx snapshotterContext) { func errorHandling(ctx snapshotterContext, err error, fatal bool) {
log.Error(err.Error()) errString := err.Error()
if ctx.SlackContext != nil { if ctx.SlackContext != nil {
err = ctx.SlackContext.SendEvent(err.Error(), log.ErrorLevel) err = ctx.SlackContext.SendEvent(errString, log.ErrorLevel)
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(fmt.Sprintf("Error while trying to send error to Slack %s", err.Error()))
} }
} }
if fatal {
log.Fatal(errString)
}
log.Error(errString)
} }