configstore: use httpError everywhere

This commit is contained in:
Simone Gotti 2019-03-14 09:34:05 +01:00
parent 8f4a5b29b9
commit 3b8a1d684b
5 changed files with 43 additions and 28 deletions

View File

@ -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
}

View File

@ -122,9 +122,8 @@ func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
org, err := h.ch.CreateOrg(ctx, &req) org, err := h.ch.CreateOrg(ctx, &req)
if err != nil { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
@ -151,10 +150,9 @@ func (h *DeleteOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
orgName := vars["orgname"] 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) h.log.Errorf("err: %+v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
} }
} }

View File

@ -120,9 +120,8 @@ func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
} }
project, err := h.ch.CreateProject(ctx, &req) project, err := h.ch.CreateProject(ctx, &req)
if err != nil { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
@ -147,10 +146,9 @@ func (h *DeleteProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
vars := mux.Vars(r) vars := mux.Vars(r)
projectID := vars["projectid"] 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) h.log.Errorf("err: %+v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
} }
} }

View File

@ -119,9 +119,8 @@ func (h *CreateRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
} }
remoteSource, err := h.ch.CreateRemoteSource(ctx, &req) remoteSource, err := h.ch.CreateRemoteSource(ctx, &req)
if err != nil { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
@ -146,10 +145,9 @@ func (h *DeleteRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
vars := mux.Vars(r) vars := mux.Vars(r)
remoteSourceName := vars["name"] 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) h.log.Errorf("err: %+v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
} }
} }

View File

@ -34,19 +34,6 @@ type UserHandler struct {
readDB *readdb.ReadDB 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 { func NewUserHandler(logger *zap.Logger, readDB *readdb.ReadDB) *UserHandler {
return &UserHandler{log: logger.Sugar(), readDB: readDB} return &UserHandler{log: logger.Sugar(), readDB: readDB}
} }