Skip to content

Commit

Permalink
Handle HostErrorSet in newUploadFailedAlert (#1487)
Browse files Browse the repository at this point in the history
Closes #1211
  • Loading branch information
ChrisSchinnerl authored Aug 28, 2024
2 parents 21dbc91 + 69221b9 commit 2efe9fc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
13 changes: 13 additions & 0 deletions worker/alerts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package worker

import (
"errors"
"time"

"go.sia.tech/core/types"
Expand Down Expand Up @@ -49,6 +50,18 @@ func newUploadFailedAlert(bucket, path, contractSet, mimeType string, minShards,
data["multipart"] = true
}

hostErr := err
for errors.Unwrap(hostErr) != nil {
hostErr = errors.Unwrap(hostErr)
}
if set, ok := hostErr.(HostErrorSet); ok {
hostErrors := make(map[string]string, len(set))
for hk, err := range set {
hostErrors[hk.String()] = err.Error()
}
data["hosts"] = hostErrors
}

return alerts.Alert{
ID: randomAlertID(),
Severity: alerts.SeverityError,
Expand Down
48 changes: 48 additions & 0 deletions worker/alerts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package worker

import (
"errors"
"fmt"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"go.sia.tech/core/types"
"go.sia.tech/renterd/alerts"
)

// TestUploadFailedAlertErrorSet is a test to verify that an upload failing with a HostErrorSet error registers an alert with all the individual errors of any host in the payload.
func TestUploadFailedAlertErrorSet(t *testing.T) {
hostErrSet := HostErrorSet{
types.PublicKey{1, 1, 1}: errors.New("test"),
}
wrapped := fmt.Errorf("wrapped error: %w", hostErrSet)

alert := newUploadFailedAlert("bucket", "path", "set", "mimeType", 1, 2, 3, true, false, wrapped)

alert.ID = types.Hash256{1, 2, 3}
alert.Timestamp = time.Time{}

expectedAlert := alerts.Alert{
ID: types.Hash256{1, 2, 3},
Severity: alerts.SeverityError,
Message: "Upload failed",
Data: map[string]any{
"bucket": "bucket",
"contractSet": "set",
"contracts": 3,
"error": wrapped.Error(),
"hosts": map[string]string{
types.PublicKey{1, 1, 1}.String(): "test",
},
"mimeType": "mimeType",
"minShards": 1,
"packing": true,
"path": "path",
"totalShards": 2,
},
}
if !cmp.Equal(alert, expectedAlert) {
t.Fatal(cmp.Diff(alert, expectedAlert))
}
}

0 comments on commit 2efe9fc

Please sign in to comment.