weldr: Improve the error message from sourceNewHandler

This commit is contained in:
Brian C. Lane 2020-03-19 15:56:01 -07:00 committed by Tom Gundersen
parent a87c0ee44d
commit 222d09499c
2 changed files with 14 additions and 4 deletions

View file

@ -354,6 +354,15 @@ func (api *API) sourceNewHandler(writer http.ResponseWriter, request *http.Reque
return
}
if request.ContentLength == 0 {
errors := responseError{
ID: "ProjectsError",
Msg: "Missing source",
}
statusResponseError(writer, http.StatusBadRequest, errors)
return
}
var source SourceConfigV0
var err error
if contentType[0] == "application/json" {
@ -366,9 +375,8 @@ func (api *API) sourceNewHandler(writer http.ResponseWriter, request *http.Reque
if err != nil {
errors := responseError{
Code: http.StatusBadRequest,
ID: "HTTPError",
Msg: "Bad Request",
ID: "ProjectsError",
Msg: "Problem parsing POST body: " + err.Error(),
}
statusResponseError(writer, http.StatusBadRequest, errors)
return

View file

@ -827,7 +827,9 @@ func TestSourcesNew(t *testing.T) {
ExpectedStatus int
ExpectedJSON string
}{
{"POST", "/api/v0/projects/source/new", ``, http.StatusBadRequest, `{"errors":[{"code":400,"id":"HTTPError","msg":"Bad Request"}],"status":false}`},
{"POST", "/api/v0/projects/source/new", ``, http.StatusBadRequest, `{"errors": [{"id": "ProjectsError","msg": "Missing source"}],"status":false}`},
// Bad JSON, missing quote after name
{"POST", "/api/v0/projects/source/new", `{"name: "fish","url": "https://download.opensuse.org/repositories/shells:/fish:/release:/3/Fedora_29/","type": "yum-baseurl","check_ssl": false,"check_gpg": false}`, http.StatusBadRequest, `{"errors": [{"id": "ProjectsError","msg": "Problem parsing POST body: invalid character 'f' after object key"}],"status":false}`},
{"POST", "/api/v0/projects/source/new", `{"name": "fish","url": "https://download.opensuse.org/repositories/shells:/fish:/release:/3/Fedora_29/","type": "yum-baseurl","check_ssl": false,"check_gpg": false}`, http.StatusOK, `{"status":true}`},
}