satellite/console/.../consoleapi: Standardize serveJSONError
This change removes all the separate implementations for `apiservice.serveJSONError()` and defines one for every service to use in `consoleapi/common.go`. Change-Id: Iabf184e5cba69a98eb25936ce11ebd07f02c8ff3
This commit is contained in:
parent
371517d93b
commit
d999a963ca
@ -72,26 +72,5 @@ func (a *Analytics) EventTriggered(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// serveJSONError writes JSON error to response output stream.
|
||||
func (a *Analytics) serveJSONError(w http.ResponseWriter, status int, err error) {
|
||||
w.WriteHeader(status)
|
||||
|
||||
if status == http.StatusNoContent {
|
||||
return
|
||||
}
|
||||
|
||||
if status == http.StatusInternalServerError {
|
||||
a.log.Error("returning internal server error to client", zap.Int("code", status), zap.Error(err))
|
||||
} else {
|
||||
a.log.Debug("returning error to client", zap.Int("code", status), zap.Error(err))
|
||||
}
|
||||
|
||||
var response struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
response.Error = err.Error()
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
a.log.Error("failed to write json error response", zap.Error(ErrAPIKeysAPI.Wrap(err)))
|
||||
}
|
||||
serveJSONError(a.log, w, status, err)
|
||||
}
|
||||
|
@ -4,7 +4,6 @@
|
||||
package consoleapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/zeebo/errs"
|
||||
@ -72,26 +71,5 @@ func (keys *APIKeys) DeleteByNameAndProjectID(w http.ResponseWriter, r *http.Req
|
||||
|
||||
// serveJSONError writes JSON error to response output stream.
|
||||
func (keys *APIKeys) serveJSONError(w http.ResponseWriter, status int, err error) {
|
||||
w.WriteHeader(status)
|
||||
|
||||
if status == http.StatusNoContent {
|
||||
return
|
||||
}
|
||||
|
||||
if status == http.StatusInternalServerError {
|
||||
keys.log.Error("returning internal server error to client", zap.Int("code", status), zap.Error(err))
|
||||
} else {
|
||||
keys.log.Debug("returning error to client", zap.Int("code", status), zap.Error(err))
|
||||
}
|
||||
|
||||
var response struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
response.Error = err.Error()
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
keys.log.Error("failed to write json error response", zap.Error(ErrAPIKeysAPI.Wrap(err)))
|
||||
}
|
||||
serveJSONError(keys.log, w, status, err)
|
||||
}
|
||||
|
@ -455,18 +455,8 @@ func (a *Auth) ResendEmail(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// serveJSONError writes JSON error to response output stream.
|
||||
func (a *Auth) serveJSONError(w http.ResponseWriter, err error) {
|
||||
w.WriteHeader(a.getStatusCode(err))
|
||||
|
||||
var response struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
response.Error = err.Error()
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
a.log.Error("failed to write json error response", zap.Error(ErrAuthAPI.Wrap(err)))
|
||||
}
|
||||
status := a.getStatusCode(err)
|
||||
serveJSONError(a.log, w, status, err)
|
||||
}
|
||||
|
||||
// getStatusCode returns http.StatusCode depends on console error class.
|
||||
|
@ -202,7 +202,8 @@ func TestDeleteAccount(t *testing.T) {
|
||||
|
||||
actualHandler := func(r *http.Request) (status int, body []byte) {
|
||||
rr := httptest.NewRecorder()
|
||||
(&consoleapi.Auth{}).DeleteAccount(rr, r)
|
||||
authController := consoleapi.NewAuth(zap.L(), nil, nil, nil, nil, nil, "", "", "", "")
|
||||
authController.DeleteAccount(rr, r)
|
||||
|
||||
//nolint:bodyclose
|
||||
result := rr.Result()
|
||||
|
@ -68,22 +68,5 @@ func (b *Buckets) AllBucketNames(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// serveJSONError writes JSON error to response output stream.
|
||||
func (b *Buckets) serveJSONError(w http.ResponseWriter, status int, err error) {
|
||||
if status == http.StatusInternalServerError {
|
||||
b.log.Error("returning error to client", zap.Int("code", status), zap.Error(err))
|
||||
} else {
|
||||
b.log.Debug("returning error to client", zap.Int("code", status), zap.Error(err))
|
||||
}
|
||||
|
||||
w.WriteHeader(status)
|
||||
|
||||
var response struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
response.Error = err.Error()
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
b.log.Error("failed to write json error response", zap.Error(ErrBucketsAPI.Wrap(err)))
|
||||
}
|
||||
serveJSONError(b.log, w, status, err)
|
||||
}
|
||||
|
40
satellite/console/consoleweb/consoleapi/common.go
Normal file
40
satellite/console/consoleweb/consoleapi/common.go
Normal file
@ -0,0 +1,40 @@
|
||||
// Copyright (C) 2021 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package consoleapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/zeebo/errs"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrUtils - console utils error type.
|
||||
ErrUtils = errs.Class("console api utils")
|
||||
)
|
||||
|
||||
// serveJSONError writes a JSON error to the response output stream.
|
||||
func serveJSONError(log *zap.Logger, w http.ResponseWriter, status int, err error) {
|
||||
switch status {
|
||||
case http.StatusNoContent:
|
||||
return
|
||||
case http.StatusInternalServerError:
|
||||
log.Error("returning error to client", zap.Int("code", status), zap.Error(err))
|
||||
case http.StatusBadRequest:
|
||||
log.Debug("returning error to client", zap.Int("code", status), zap.Error(err))
|
||||
default:
|
||||
log.Info("returning error to client", zap.Int("code", status), zap.Error(err))
|
||||
}
|
||||
|
||||
w.WriteHeader(status)
|
||||
|
||||
err = json.NewEncoder(w).Encode(map[string]string{
|
||||
"error": err.Error(),
|
||||
})
|
||||
if err != nil {
|
||||
log.Error("failed to write json error response", zap.Error(ErrUtils.Wrap(err)))
|
||||
}
|
||||
}
|
@ -310,22 +310,5 @@ func (p *Payments) TokenDeposit(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// serveJSONError writes JSON error to response output stream.
|
||||
func (p *Payments) serveJSONError(w http.ResponseWriter, status int, err error) {
|
||||
if status == http.StatusInternalServerError {
|
||||
p.log.Error("returning error to client", zap.Int("code", status), zap.Error(err))
|
||||
} else {
|
||||
p.log.Debug("returning error to client", zap.Int("code", status), zap.Error(err))
|
||||
}
|
||||
|
||||
w.WriteHeader(status)
|
||||
|
||||
var response struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
response.Error = err.Error()
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
p.log.Error("failed to write json error response", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
||||
}
|
||||
serveJSONError(p.log, w, status, err)
|
||||
}
|
||||
|
@ -103,18 +103,5 @@ func (ul *UsageLimits) TotalUsageLimits(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
// serveJSONError writes JSON error to response output stream.
|
||||
func (ul *UsageLimits) serveJSONError(w http.ResponseWriter, status int, err error) {
|
||||
ul.log.Error("returning error to client", zap.Int("code", status), zap.Error(err))
|
||||
|
||||
w.WriteHeader(status)
|
||||
|
||||
var response struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
response.Error = err.Error()
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
ul.log.Error("failed to write json error response", zap.Error(ErrUsageLimitsAPI.Wrap(err)))
|
||||
}
|
||||
serveJSONError(ul.log, w, status, err)
|
||||
}
|
Loading…
Reference in New Issue
Block a user