From 22a0452ea97aa6adc885cf451c79135d3ead106e Mon Sep 17 00:00:00 2001 From: Sanne Raymaekers Date: Mon, 2 Sep 2024 12:30:52 +0200 Subject: [PATCH] osbuild-worker: handle error wrapping from dnfjson package osbuild/images#751 wrapped the errors in the images/dnfjson package to provide more details, the depsolve job should take this into account to map the dnfjson error to the correct worker client error. This caused user input errors errors to be misclassified as internal errors, triggering depsolve job failure alerts. --- cmd/osbuild-worker/jobimpl-depsolve.go | 13 +++++++------ cmd/osbuild-worker/jobimpl-depsolve_test.go | 5 +++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cmd/osbuild-worker/jobimpl-depsolve.go b/cmd/osbuild-worker/jobimpl-depsolve.go index 39cb5c878..ac1405a33 100644 --- a/cmd/osbuild-worker/jobimpl-depsolve.go +++ b/cmd/osbuild-worker/jobimpl-depsolve.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "net/url" "strings" @@ -78,12 +79,12 @@ func workerClientErrorFrom(err error, logWithId *logrus.Entry) *clienterrors.Err logWithId.Errorf("workerClientErrorFrom expected an error to be processed. Not nil") } - switch e := err.(type) { - case dnfjson.Error: + var dnfjsonErr dnfjson.Error + if errors.As(err, &dnfjsonErr) { // Error originates from dnf-json - reason := fmt.Sprintf("DNF error occurred: %s", e.Kind) - details := e.Reason - switch e.Kind { + reason := fmt.Sprintf("DNF error occurred: %s", dnfjsonErr.Kind) + details := dnfjsonErr.Reason + switch dnfjsonErr.Kind { case "DepsolveError": return clienterrors.New(clienterrors.ErrorDNFDepsolveError, reason, details) case "MarkingErrors": @@ -96,7 +97,7 @@ func workerClientErrorFrom(err error, logWithId *logrus.Entry) *clienterrors.Err // by dnf-json and not explicitly handled here. return clienterrors.New(clienterrors.ErrorDNFOtherError, reason, details) } - default: + } else { reason := "rpmmd error in depsolve job" details := fmt.Sprintf("%v", err) // Error originates from internal/rpmmd, not from dnf-json diff --git a/cmd/osbuild-worker/jobimpl-depsolve_test.go b/cmd/osbuild-worker/jobimpl-depsolve_test.go index 41e494fe7..b771572aa 100644 --- a/cmd/osbuild-worker/jobimpl-depsolve_test.go +++ b/cmd/osbuild-worker/jobimpl-depsolve_test.go @@ -26,6 +26,11 @@ func TestWorkerClientErrorFromDnfJson(t *testing.T) { entry, hook := makeMockEntry() clientErr := worker.WorkerClientErrorFrom(dnfJsonErr, entry) assert.Equal(t, `Code: 20, Reason: DNF error occurred: DepsolveError, Details: something is terribly wrong`, clientErr.String()) + + wrappedErr := fmt.Errorf("Wrap the error: %w", dnfJsonErr) + clientErr = worker.WorkerClientErrorFrom(wrappedErr, entry) + assert.Equal(t, `Code: 20, Reason: DNF error occurred: DepsolveError, Details: something is terribly wrong`, clientErr.String()) + assert.Equal(t, 0, len(hook.AllEntries())) }