rework not found

This commit is contained in:
Mikaël Cluseau 2023-02-13 18:07:10 +01:00
parent 4acdf88785
commit 5a6c0fa3d8
5 changed files with 29 additions and 29 deletions

View File

@ -294,7 +294,7 @@ func (s KVSecrets[T]) Put(key string, v T) (err error) {
func (s KVSecrets[T]) WsList(resp *restful.Response, prefix string) { func (s KVSecrets[T]) WsList(resp *restful.Response, prefix string) {
keys, err := s.Keys(prefix) keys, err := s.Keys(prefix)
if err != nil { if err != nil {
httperr.New(http.StatusInternalServerError, err).WriteJSON(resp.ResponseWriter) wsError(resp, err)
return return
} }
@ -304,12 +304,12 @@ func (s KVSecrets[T]) WsList(resp *restful.Response, prefix string) {
func (s KVSecrets[T]) WsGet(resp *restful.Response, key string) { func (s KVSecrets[T]) WsGet(resp *restful.Response, key string) {
keys, found, err := s.Get(key) keys, found, err := s.Get(key)
if err != nil { if err != nil {
httperr.New(http.StatusInternalServerError, err).WriteJSON(resp.ResponseWriter) wsError(resp, err)
return return
} }
if !found { if !found {
ErrNotFound.WriteJSON(resp.ResponseWriter) wsNotFound(resp)
return return
} }
@ -320,13 +320,13 @@ func (s KVSecrets[T]) WsPut(req *restful.Request, resp *restful.Response, key st
v := new(T) v := new(T)
err := req.ReadEntity(v) err := req.ReadEntity(v)
if err != nil { if err != nil {
httperr.New(http.StatusBadRequest, err).WriteJSON(resp.ResponseWriter) wsBadRequest(resp, err.Error())
return return
} }
err = s.Put(key, *v) err = s.Put(key, *v)
if err != nil { if err != nil {
httperr.New(http.StatusInternalServerError, err).WriteJSON(resp.ResponseWriter) wsError(resp, err)
return return
} }
} }

View File

@ -13,7 +13,7 @@ var clusterSecretKVs = []string{}
func newClusterSecretKV[T any](name string) KVSecrets[T] { func newClusterSecretKV[T any](name string) KVSecrets[T] {
clusterSecretKVs = append(clusterSecretKVs, name) clusterSecretKVs = append(clusterSecretKVs, name)
return KVSecrets[T]{"clusters/"+name} return KVSecrets[T]{"clusters/" + name}
} }
func wsListClusters(req *restful.Request, resp *restful.Response) { func wsListClusters(req *restful.Request, resp *restful.Response) {
@ -40,7 +40,7 @@ func wsReadCluster(req *restful.Request, resp *restful.Response) (cluster *local
cluster = cfg.Cluster(clusterName) cluster = cfg.Cluster(clusterName)
if cluster == nil { if cluster == nil {
wsNotFound(req, resp) wsNotFound(resp)
return return
} }
@ -64,7 +64,7 @@ func wsClusterAddons(req *restful.Request, resp *restful.Response) {
if len(cluster.Addons) == 0 { if len(cluster.Addons) == 0 {
log.Printf("cluster %q has no addons defined", cluster.Name) log.Printf("cluster %q has no addons defined", cluster.Name)
wsNotFound(req, resp) wsNotFound(resp)
return return
} }
@ -85,13 +85,13 @@ func wsClusterAddons(req *restful.Request, resp *restful.Response) {
func wsClusterCACert(req *restful.Request, resp *restful.Response) { func wsClusterCACert(req *restful.Request, resp *restful.Response) {
cs := secretData.clusters[req.PathParameter("cluster-name")] cs := secretData.clusters[req.PathParameter("cluster-name")]
if cs == nil { if cs == nil {
wsNotFound(req, resp) wsNotFound(resp)
return return
} }
ca := cs.CAs[req.PathParameter("ca-name")] ca := cs.CAs[req.PathParameter("ca-name")]
if ca == nil { if ca == nil {
wsNotFound(req, resp) wsNotFound(resp)
return return
} }
@ -101,13 +101,13 @@ func wsClusterCACert(req *restful.Request, resp *restful.Response) {
func wsClusterSignedCert(req *restful.Request, resp *restful.Response) { func wsClusterSignedCert(req *restful.Request, resp *restful.Response) {
cs := secretData.clusters[req.PathParameter("cluster-name")] cs := secretData.clusters[req.PathParameter("cluster-name")]
if cs == nil { if cs == nil {
wsNotFound(req, resp) wsNotFound(resp)
return return
} }
ca := cs.CAs[req.PathParameter("ca-name")] ca := cs.CAs[req.PathParameter("ca-name")]
if ca == nil { if ca == nil {
wsNotFound(req, resp) wsNotFound(resp)
return return
} }
@ -127,7 +127,7 @@ func wsClusterSignedCert(req *restful.Request, resp *restful.Response) {
kc := ca.Signed[name] kc := ca.Signed[name]
if kc == nil { if kc == nil {
wsNotFound(req, resp) wsNotFound(resp)
return return
} }

View File

@ -58,7 +58,7 @@ func wsDownload(req *restful.Request, resp *restful.Response) {
asset := req.PathParameter("asset") asset := req.PathParameter("asset")
if token == "" || asset == "" { if token == "" || asset == "" {
wsNotFound(req, resp) wsNotFound(resp)
return return
} }
@ -95,7 +95,7 @@ func wsDownload(req *restful.Request, resp *restful.Response) {
}) })
if !found { if !found {
wsNotFound(req, resp) wsNotFound(resp)
return return
} }
@ -115,7 +115,7 @@ func wsDownload(req *restful.Request, resp *restful.Response) {
case "cluster": case "cluster":
cluster := cfg.ClusterByName(spec.Name) cluster := cfg.ClusterByName(spec.Name)
if cluster == nil { if cluster == nil {
wsNotFound(req, resp) wsNotFound(resp)
return return
} }
@ -125,13 +125,13 @@ func wsDownload(req *restful.Request, resp *restful.Response) {
resp.Write([]byte(cluster.Addons)) resp.Write([]byte(cluster.Addons))
default: default:
wsNotFound(req, resp) wsNotFound(resp)
} }
case "host": case "host":
host := cfg.Host(spec.Name) host := cfg.Host(spec.Name)
if host == nil { if host == nil {
wsNotFound(req, resp) wsNotFound(resp)
return return
} }
@ -145,6 +145,6 @@ func wsDownload(req *restful.Request, resp *restful.Response) {
renderHost(resp.ResponseWriter, req.Request, asset, host, cfg) renderHost(resp.ResponseWriter, req.Request, asset, host, cfg)
default: default:
wsNotFound(req, resp) wsNotFound(resp)
} }
} }

View File

@ -111,7 +111,7 @@ func (ws *wsHost) host(req *restful.Request, resp *restful.Response) (host *loca
return return
} }
if hostname == "" { if hostname == "" {
wsNotFound(req, resp) wsNotFound(resp)
return return
} }
@ -124,7 +124,7 @@ func (ws *wsHost) host(req *restful.Request, resp *restful.Response) (host *loca
host = cfg.Host(hostname) host = cfg.Host(hostname)
if host == nil { if host == nil {
log.Print("no host named ", hostname) log.Print("no host named ", hostname)
wsNotFound(req, resp) wsNotFound(resp)
return return
} }
return return

View File

@ -220,8 +220,8 @@ func wsReadConfig(resp *restful.Response) *localconfig.Config {
return cfg return cfg
} }
func wsNotFound(req *restful.Request, resp *restful.Response) { func wsNotFound(resp *restful.Response) {
http.NotFound(resp.ResponseWriter, req.Request) wsError(resp, ErrNotFound)
} }
func wsBadRequest(resp *restful.Response, err string) { func wsBadRequest(resp *restful.Response, err string) {