weldr: Return a JSON API error response for all unknown requests

This matches the lorax-composer behavior.
This commit is contained in:
Brian C. Lane 2020-03-26 16:52:13 -07:00 committed by Tom Gundersen
parent e3934108f7
commit fc205786fc
3 changed files with 10 additions and 5 deletions

View file

@ -29,10 +29,10 @@ func TestRequest(t *testing.T) {
t.Fatalf("Request bad route: %d != 404", resp.StatusCode)
}
// Test that apiError returns an error when trying to parse non-JSON response
// Test that apiError returns an error response
_, err = apiError(resp)
if err == nil {
t.Fatalf("apiError of a 404 response did not return an error: %#v", resp)
if err != nil {
t.Fatalf("apiError could not parse the response: %s", err)
}
// Make a request with a bad offset to trigger a JSON response with Status set to 400

View file

@ -167,7 +167,12 @@ func methodNotAllowedHandler(writer http.ResponseWriter, request *http.Request)
}
func notFoundHandler(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusNotFound)
errors := responseError{
Code: http.StatusNotFound,
ID: "HTTPError",
Msg: "Not Found",
}
statusResponseError(writer, http.StatusNotFound, errors)
}
func notImplementedHandler(writer http.ResponseWriter, httpRequest *http.Request, _ httprouter.Params) {

View file

@ -51,7 +51,7 @@ func TestBasic(t *testing.T) {
{"/api/v0/projects/source/list", http.StatusOK, `{"sources":["test-id"]}`},
{"/api/v0/projects/source/info", http.StatusNotFound, ``},
{"/api/v0/projects/source/info", http.StatusNotFound, `{"errors":[{"code":404,"id":"HTTPError","msg":"Not Found"}],"status":false}`},
{"/api/v0/projects/source/info/", http.StatusNotFound, `{"errors":[{"code":404,"id":"HTTPError","msg":"Not Found"}],"status":false}`},
{"/api/v0/projects/source/info/foo", http.StatusOK, `{"errors":[{"id":"UnknownSource","msg":"foo is not a valid source"}],"sources":{}}`},
{"/api/v0/projects/source/info/test-id", http.StatusOK, `{"sources":{"test-id":{"name":"test-id","type":"yum-baseurl","url":"http://example.com/test/os/x86_64","check_gpg":true,"check_ssl":true,"system":true}},"errors":[]}`},