This is an automated email from the ASF dual-hosted git repository.
rawlin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git
The following commit(s) were added to refs/heads/master by this push:
new 831ca34 Remove deprecated error handler and references (#6431)
831ca34 is described below
commit 831ca343379d76a827bb12a5f1d00ee72daa8e48
Author: ocket8888 <[email protected]>
AuthorDate: Tue Mar 22 13:12:12 2022 -0600
Remove deprecated error handler and references (#6431)
---
lib/go-tc/alerts.go | 33 ------------------------
lib/go-tc/alerts_test.go | 26 -------------------
traffic_ops/traffic_ops_golang/api/api.go | 18 ++++++++-----
traffic_ops/traffic_ops_golang/login/login.go | 9 +++----
traffic_ops/traffic_ops_golang/routing/routes.go | 8 ++----
5 files changed, 18 insertions(+), 76 deletions(-)
diff --git a/lib/go-tc/alerts.go b/lib/go-tc/alerts.go
index f65ba00..386d108 100644
--- a/lib/go-tc/alerts.go
+++ b/lib/go-tc/alerts.go
@@ -20,14 +20,7 @@ package tc
*/
import (
- "context"
- "encoding/json"
- "fmt"
- "net/http"
"strings"
-
- "github.com/apache/trafficcontrol/lib/go-log"
- "github.com/apache/trafficcontrol/lib/go-rfc"
)
// Alert represents an informational message, typically returned through the
Traffic Ops API.
@@ -67,32 +60,6 @@ func CreateAlerts(level AlertLevel, messages ...string)
Alerts {
return Alerts{alerts}
}
-// GetHandleErrorsFunc is used to provide an error-handling function. The
error handler provides a
-// response to an HTTP request made to the Traffic Ops API and accepts a
response code and a set of
-// errors to display as alerts.
-//
-// Deprecated: Traffic Ops API handlers should use
-//
github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api.HandleErr
instead.
-func GetHandleErrorsFunc(w http.ResponseWriter, r *http.Request) func(status
int, errs ...error) {
- return func(status int, errs ...error) {
- log.Errorf("%v %v\n", r.RemoteAddr, errs)
- errBytes, jsonErr := json.Marshal(CreateErrorAlerts(errs...))
- if jsonErr != nil {
- log.Errorf("failed to marshal error: %s\n", jsonErr)
- w.WriteHeader(http.StatusInternalServerError)
- fmt.Fprintf(w,
http.StatusText(http.StatusInternalServerError))
- return
- }
- w.Header().Set(rfc.ContentType, rfc.ApplicationJSON)
-
- ctx := r.Context()
- ctx = context.WithValue(ctx, StatusKey, status)
- *r = *r.WithContext(ctx)
-
- fmt.Fprintf(w, "%s", errBytes)
- }
-}
-
// ToStrings converts Alerts to a slice of strings that are their messages.
Note that this return
// value doesn't contain their Levels anywhere.
func (alerts *Alerts) ToStrings() []string {
diff --git a/lib/go-tc/alerts_test.go b/lib/go-tc/alerts_test.go
index a4ea563..9571eed 100644
--- a/lib/go-tc/alerts_test.go
+++ b/lib/go-tc/alerts_test.go
@@ -22,8 +22,6 @@ package tc
import (
"errors"
"fmt"
- "net/http"
- "net/http/httptest"
"reflect"
"testing"
)
@@ -122,30 +120,6 @@ func ExampleAlerts_ErrorString() {
//
}
-func TestGetHandleErrorFunc(t *testing.T) {
- w := httptest.NewRecorder()
- r, err := http.NewRequest("", ".", nil)
- if err != nil {
- t.Error("Error creating new request")
- }
- body := `{"alerts":[{"text":"this is an error","level":"error"}]}`
-
- errHandler := GetHandleErrorsFunc(w, r)
- errHandler(http.StatusBadRequest, fmt.Errorf("this is an error"))
- if w.Body.String() != body {
- t.Error("Expected body", body, "got", w.Body.String())
- }
-
- w = httptest.NewRecorder()
- body = `{"alerts":[]}`
-
- errHandler = GetHandleErrorsFunc(w, r)
- errHandler(http.StatusBadRequest, nil)
- if w.Body.String() != body {
- t.Error("Expected body", body, "got", w.Body.String())
- }
-}
-
func TestCreateAlerts(t *testing.T) {
expected := Alerts{[]Alert{}}
alerts := CreateAlerts(WarnLevel)
diff --git a/traffic_ops/traffic_ops_golang/api/api.go
b/traffic_ops/traffic_ops_golang/api/api.go
index 0a7d3ce..74fd3d8 100644
--- a/traffic_ops/traffic_ops_golang/api/api.go
+++ b/traffic_ops/traffic_ops_golang/api/api.go
@@ -127,8 +127,11 @@ func WriteAndLogErr(w http.ResponseWriter, r
*http.Request, bts []byte) {
}
}
-// WriteResp takes any object, serializes it as JSON, and writes that to w.
Any errors are logged and written to w via tc.GetHandleErrorsFunc.
-// This is a helper for the common case; not using this in unusual cases is
perfectly acceptable.
+// WriteResp takes any object, serializes it as JSON, and writes that to w.
+//
+// Any errors are logged and written to w as alerts (if applicable). This is a
+// helper for the common case; not using this in unusual cases is perfectly
+// acceptable.
func WriteResp(w http.ResponseWriter, r *http.Request, v interface{}) {
resp := APIResponse{v}
WriteRespRaw(w, r, resp)
@@ -145,7 +148,7 @@ func WriteRespRaw(w http.ResponseWriter, r *http.Request, v
interface{}) {
respBts, err := json.Marshal(v)
if err != nil {
log.Errorf("marshalling JSON (raw) for %T: %v", v, err)
- tc.GetHandleErrorsFunc(w, r)(http.StatusInternalServerError,
errors.New(http.StatusText(http.StatusInternalServerError)))
+ handleSimpleErr(w, r, http.StatusInternalServerError, nil, nil)
return
}
w.Header().Set(rfc.ContentType, rfc.ApplicationJSON)
@@ -177,7 +180,7 @@ func WriteRespVals(w http.ResponseWriter, r *http.Request,
v interface{}, vals m
respBts, err := json.Marshal(vals)
if err != nil {
log.Errorf("marshalling JSON for %T: %v", v, err)
- tc.GetHandleErrorsFunc(w, r)(http.StatusInternalServerError,
errors.New(http.StatusText(http.StatusInternalServerError)))
+ handleSimpleErr(w, r, http.StatusInternalServerError, nil, nil)
return
}
w.Header().Set("Content-Type", "application/json")
@@ -305,8 +308,11 @@ func RespWriterVals(w http.ResponseWriter, r
*http.Request, tx *sql.Tx, vals map
}
}
-// WriteRespAlert creates an alert, serializes it as JSON, and writes that to
w. Any errors are logged and written to w via tc.GetHandleErrorsFunc.
-// This is a helper for the common case; not using this in unusual cases is
perfectly acceptable.
+// WriteRespAlert creates an alert, serializes it as JSON, and writes that to
w.
+//
+// Any errors are logged and written to w as alerts (if applicable). This is a
+// helper for the common case; not using this in unusual cases is perfectly
+// acceptable.
func WriteRespAlert(w http.ResponseWriter, r *http.Request, level
tc.AlertLevel, msg string) {
if respWritten(r) {
log.Errorf("WriteRespAlert called after a write already
occurred! Not double-writing! Path %s", r.URL.Path)
diff --git a/traffic_ops/traffic_ops_golang/login/login.go
b/traffic_ops/traffic_ops_golang/login/login.go
index 2afcd45..37cfe7e 100644
--- a/traffic_ops/traffic_ops_golang/login/login.go
+++ b/traffic_ops/traffic_ops_golang/login/login.go
@@ -110,12 +110,11 @@ Subject: {{.InstanceName}} Password Reset Request` +
"\r\n\r" + `
func LoginHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
- handleErrs := tc.GetHandleErrorsFunc(w, r)
defer r.Body.Close()
authenticated := false
form := auth.PasswordForm{}
if err := json.NewDecoder(r.Body).Decode(&form); err != nil {
- handleErrs(http.StatusBadRequest, err)
+ api.HandleErr(w, r, nil, http.StatusBadRequest, err,
nil)
return
}
if form.Username == "" || form.Password == "" {
@@ -225,7 +224,7 @@ func LoginHandler(db *sqlx.DB, cfg config.Config)
http.HandlerFunc {
}
respBts, err := json.Marshal(resp)
if err != nil {
- handleErrs(http.StatusInternalServerError, err)
+ api.HandleErr(w, r, nil,
http.StatusInternalServerError, nil, err)
return
}
w.Header().Set(rfc.ContentType, rfc.ApplicationJSON)
@@ -432,7 +431,7 @@ func OauthLoginHandler(db *sqlx.DB, cfg config.Config)
http.HandlerFunc {
ljwt.WithJWKSetFetcher(jwksFetcher),
)
if err != nil {
- api.HandleErr(w, r, nil,
http.StatusInternalServerError, nil, errors.New("Error decoding token with
message: "+err.Error()))
+ api.HandleErr(w, r, nil,
http.StatusInternalServerError, nil, fmt.Errorf("Error decoding token with
message: %w", err))
return
}
@@ -470,7 +469,7 @@ func OauthLoginHandler(db *sqlx.DB, cfg config.Config)
http.HandlerFunc {
respBts, err := json.Marshal(resp)
if err != nil {
- api.HandleErr(w, r, nil,
http.StatusInternalServerError, nil, err)
+ api.HandleErr(w, r, nil,
http.StatusInternalServerError, nil, fmt.Errorf("encoding response: %w", err))
return
}
w.Header().Set(rfc.ContentType, rfc.ApplicationJSON)
diff --git a/traffic_ops/traffic_ops_golang/routing/routes.go
b/traffic_ops/traffic_ops_golang/routing/routes.go
index 8345a4d..c51e116 100644
--- a/traffic_ops/traffic_ops_golang/routing/routes.go
+++ b/traffic_ops/traffic_ops_golang/routing/routes.go
@@ -1314,14 +1314,12 @@ func Routes(d ServerData) ([]Route, http.Handler,
error) {
func MemoryStatsHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
- handleErrs := tc.GetHandleErrorsFunc(w, r)
stats := runtime.MemStats{}
runtime.ReadMemStats(&stats)
bytes, err := json.Marshal(stats)
if err != nil {
- log.Errorln("unable to marshal stats: " + err.Error())
- handleErrs(http.StatusInternalServerError,
errors.New("marshalling error"))
+ api.HandleErr(w, r, nil,
http.StatusInternalServerError, nil, fmt.Errorf("unable to marshal stats: %w",
err))
return
}
w.Header().Set("Content-Type", "application/json")
@@ -1331,13 +1329,11 @@ func MemoryStatsHandler() http.HandlerFunc {
func DBStatsHandler(db *sqlx.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
- handleErrs := tc.GetHandleErrorsFunc(w, r)
stats := db.DB.Stats()
bytes, err := json.Marshal(stats)
if err != nil {
- log.Errorln("unable to marshal stats: " + err.Error())
- handleErrs(http.StatusInternalServerError,
errors.New("marshalling error"))
+ api.HandleErr(w, r, nil,
http.StatusInternalServerError, nil, fmt.Errorf("unable to marshal stats: %w",
err))
return
}
w.Header().Set("Content-Type", "application/json")