CloudAPI: Invalid package set returns 400

Previously a bad error code was returned, fixes #1477.

Testing:

I have two test cases to test the solution. The first is a request that
makes depsolve crash by changing the dnf-json script by an almost empty
one that only throws an exception. The second one fails because it
requests a non existing package. The former ends with a 500 error and
the later with a 400.

----8<-----

HTTP/1.1 500 Internal Server Error

Failed to depsolve base packages for ami/x86_64/centos-8: ailed to
depsolve base packages for ami/x86_64/centos-8: unexpected end of JSON
input

----8<-----

HTTP/1.1 400 Bad Request

Content-Length: 226Failed to depsolve base packages for
ami/x86_64/centos-8: DNF error occured: MarkingErrors: Error occurred
when marking packages for installation: Problems in request:
missing packages: jesuisunpaquetquinexistepas_idonotexist
This commit is contained in:
Thomas Lavocat 2021-07-16 14:34:41 +02:00 committed by Sanne Raymaekers
parent 76b2579c4b
commit cfc91a3eb6
2 changed files with 42 additions and 1 deletions

View file

@ -182,7 +182,17 @@ func (server *Server) Compose(w http.ResponseWriter, r *http.Request) {
for name, packages := range packageSets {
pkgs, _, err := server.rpmMetadata.Depsolve(packages, repositories, distribution.ModulePlatformID(), arch.Name())
if err != nil {
http.Error(w, fmt.Sprintf("Failed to depsolve base packages for %s/%s/%s: %s", ir.ImageType, ir.Architecture, request.Distribution, err), http.StatusInternalServerError)
var error_type int
switch err.(type) {
// Known DNF errors falls under BadRequest
case *rpmmd.DNFError:
error_type = http.StatusBadRequest
// All other kind of errors are internal server Errors.
// (json marshalling issues for instance)
case error:
error_type = http.StatusInternalServerError
}
http.Error(w, fmt.Sprintf("Failed to depsolve base packages for %s/%s/%s: %s", ir.ImageType, ir.Architecture, request.Distribution, err), error_type)
return
}
pkgSpecSets[name] = pkgs