diff --git a/internal/services/configstore/api/api.go b/internal/services/configstore/api/api.go new file mode 100644 index 0000000..02d4e5a --- /dev/null +++ b/internal/services/configstore/api/api.go @@ -0,0 +1,34 @@ +// Copyright 2019 Sorint.lab +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied +// See the License for the specific language governing permissions and +// limitations under the License. + +package api + +import ( + "net/http" + + "github.com/sorintlab/agola/internal/util" +) + +func httpError(w http.ResponseWriter, err error) bool { + if err != nil { + if util.IsErrBadRequest(err) { + http.Error(w, err.Error(), http.StatusBadRequest) + } else { + http.Error(w, "", http.StatusInternalServerError) + } + return true + } + + return false +} diff --git a/internal/services/configstore/api/org.go b/internal/services/configstore/api/org.go index 5529b5e..41e1a80 100644 --- a/internal/services/configstore/api/org.go +++ b/internal/services/configstore/api/org.go @@ -122,9 +122,8 @@ func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } org, err := h.ch.CreateOrg(ctx, &req) - if err != nil { + if httpError(w, err) { h.log.Errorf("err: %+v", err) - http.Error(w, err.Error(), http.StatusBadRequest) return } @@ -151,10 +150,9 @@ func (h *DeleteOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) orgName := vars["orgname"] - if err := h.ch.DeleteOrg(ctx, orgName); err != nil { + err := h.ch.DeleteOrg(ctx, orgName) + if httpError(w, err) { h.log.Errorf("err: %+v", err) - http.Error(w, err.Error(), http.StatusBadRequest) - return } } diff --git a/internal/services/configstore/api/project.go b/internal/services/configstore/api/project.go index e7caac6..c8e281f 100644 --- a/internal/services/configstore/api/project.go +++ b/internal/services/configstore/api/project.go @@ -120,9 +120,8 @@ func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) } project, err := h.ch.CreateProject(ctx, &req) - if err != nil { + if httpError(w, err) { h.log.Errorf("err: %+v", err) - http.Error(w, err.Error(), http.StatusBadRequest) return } @@ -147,10 +146,9 @@ func (h *DeleteProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) vars := mux.Vars(r) projectID := vars["projectid"] - if err := h.ch.DeleteProject(ctx, projectID); err != nil { + err := h.ch.DeleteProject(ctx, projectID) + if httpError(w, err) { h.log.Errorf("err: %+v", err) - http.Error(w, err.Error(), http.StatusBadRequest) - return } } diff --git a/internal/services/configstore/api/remotesource.go b/internal/services/configstore/api/remotesource.go index 01fb5e5..61254f0 100644 --- a/internal/services/configstore/api/remotesource.go +++ b/internal/services/configstore/api/remotesource.go @@ -119,9 +119,8 @@ func (h *CreateRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Req } remoteSource, err := h.ch.CreateRemoteSource(ctx, &req) - if err != nil { + if httpError(w, err) { h.log.Errorf("err: %+v", err) - http.Error(w, err.Error(), http.StatusBadRequest) return } @@ -146,10 +145,9 @@ func (h *DeleteRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Req vars := mux.Vars(r) remoteSourceName := vars["name"] - if err := h.ch.DeleteRemoteSource(ctx, remoteSourceName); err != nil { + err := h.ch.DeleteRemoteSource(ctx, remoteSourceName) + if httpError(w, err) { h.log.Errorf("err: %+v", err) - http.Error(w, err.Error(), http.StatusBadRequest) - return } } diff --git a/internal/services/configstore/api/user.go b/internal/services/configstore/api/user.go index 8fec6d6..224b4e0 100644 --- a/internal/services/configstore/api/user.go +++ b/internal/services/configstore/api/user.go @@ -34,19 +34,6 @@ type UserHandler struct { readDB *readdb.ReadDB } -func httpError(w http.ResponseWriter, err error) bool { - if err != nil { - if util.IsErrBadRequest(err) { - http.Error(w, err.Error(), http.StatusBadRequest) - } else { - http.Error(w, "", http.StatusInternalServerError) - } - return true - } - - return false -} - func NewUserHandler(logger *zap.Logger, readDB *readdb.ReadDB) *UserHandler { return &UserHandler{log: logger.Sugar(), readDB: readDB} }