osbuild-worker: implement structured errors

Implement the structured errors as defined by the worker client.
Every error for each of the job types now returns a structured
error with a reason and a specific error code.  This will make
it possible to differentiate between 4xx errors and 5xx errors.

This commit refactors the way errors are implemented in the workers,
but maintains backwards compatability in composer by checking for
both kinds of errors.
This commit is contained in:
Gianluca Zuccarelli 2021-12-23 13:45:30 +00:00 committed by Sanne Raymaekers
parent daf24f8db3
commit cc981b887a
8 changed files with 142 additions and 68 deletions

View file

@ -5,6 +5,7 @@ import (
"github.com/osbuild/osbuild-composer/internal/rpmmd"
"github.com/osbuild/osbuild-composer/internal/worker"
"github.com/osbuild/osbuild-composer/internal/worker/clienterrors"
)
type DepsolveJobImpl struct {
@ -40,13 +41,23 @@ func (impl *DepsolveJobImpl) Run(job worker.Job) error {
var result worker.DepsolveJobResult
result.PackageSpecs, err = impl.depsolve(args.PackageSets, args.Repos, args.ModulePlatformID, args.Arch, args.Releasever, args.PackageSetsRepositories)
if err != nil {
switch err.(type) {
switch e := err.(type) {
case *rpmmd.DNFError:
result.ErrorType = worker.DepsolveErrorType
// Error originates from dnf-json (the http call dnf-json wasn't StatusOK)
switch e.Kind {
case "DepsolveError":
result.JobError = clienterrors.WorkerClientError(clienterrors.ErrorDNFDepsolveError, err.Error())
case "MarkingError":
result.JobError = clienterrors.WorkerClientError(clienterrors.ErrorDNFMarkingError, err.Error())
default:
// This still has the kind/reason format but a kind that's returned
// by dnf-json and not explicitly handled here.
result.JobError = clienterrors.WorkerClientError(clienterrors.ErrorDNFOtherError, err.Error())
}
case error:
result.ErrorType = worker.OtherErrorType
// Error originates from internal/rpmmd, not from dnf-json
result.JobError = clienterrors.WorkerClientError(clienterrors.ErrorRPMMDError, err.Error())
}
result.Error = err.Error()
}
err = job.Update(&result)