You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by oc...@apache.org on 2020/01/24 13:46:52 UTC

[trafficcontrol] branch master updated: Fix API capabilities rewrite bugs (#4321)

This is an automated email from the ASF dual-hosted git repository.

ocket8888 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 76db3a8  Fix API capabilities rewrite bugs (#4321)
76db3a8 is described below

commit 76db3a830d0605466c4aeaab39884fced854b415
Author: Michael Hoppal <54...@users.noreply.github.com>
AuthorDate: Fri Jan 24 06:46:44 2020 -0700

    Fix API capabilities rewrite bugs (#4321)
    
    Include:
    - Empty list instead of null when filtering to non existing
    capabilities
    - Return 400 instead of 500 when passing in an invalid http method
---
 traffic_ops/traffic_ops_golang/api/api.go                  | 14 ++++++++++++++
 .../traffic_ops_golang/apicapability/api_capabilities.go   | 14 ++++++--------
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/traffic_ops/traffic_ops_golang/api/api.go b/traffic_ops/traffic_ops_golang/api/api.go
index 182e019..7112045 100644
--- a/traffic_ops/traffic_ops_golang/api/api.go
+++ b/traffic_ops/traffic_ops_golang/api/api.go
@@ -690,6 +690,16 @@ func parseUniqueConstraint(err *pq.Error) (error, error, int) {
 	return fmt.Errorf("%v %s '%s' already exists.", err.Table, match[1], match[2]), nil, http.StatusBadRequest
 }
 
+// parses pq errors for database enum constraint violations
+func parseEnumConstraint(err *pq.Error) (error, error, int) {
+	pattern := regexp.MustCompile(`invalid input value for enum (.+): \"(.+)\"`)
+	match := pattern.FindStringSubmatch(err.Message)
+	if match == nil {
+		return nil, nil, http.StatusOK
+	}
+	return fmt.Errorf("invalid enum value %s for field %s.", match[2], match[1]), nil, http.StatusBadRequest
+}
+
 // parses pq errors for ON DELETE RESTRICT fk constraint violations
 //
 // Note: This method would also catch an ON UPDATE RESTRICT fk constraint,
@@ -752,6 +762,10 @@ func ParseDBError(ierr error) (error, error, int) {
 		return usrErr, sysErr, errCode
 	}
 
+	if usrErr, sysErr, errCode := parseEnumConstraint(err); errCode != http.StatusOK {
+		return usrErr, sysErr, errCode
+	}
+
 	return nil, err, http.StatusInternalServerError
 }
 
diff --git a/traffic_ops/traffic_ops_golang/apicapability/api_capabilities.go b/traffic_ops/traffic_ops_golang/apicapability/api_capabilities.go
index f96edbc..949198a 100644
--- a/traffic_ops/traffic_ops_golang/apicapability/api_capabilities.go
+++ b/traffic_ops/traffic_ops_golang/apicapability/api_capabilities.go
@@ -42,7 +42,7 @@ func GetAPICapabilitiesHandler(w http.ResponseWriter, r *http.Request) {
 	defer inf.Close()
 
 	results, errCode, usrErr, sysErr := getAPICapabilities(inf.Tx, inf.Params)
-	if userErr != nil || sysErr != nil {
+	if usrErr != nil || sysErr != nil {
 		api.HandleErr(w, r, inf.Tx.Tx, errCode, usrErr, sysErr)
 		return
 	}
@@ -68,7 +68,7 @@ func getAPICapabilities(tx *sqlx.Tx, params map[string]string) ([]tc.APICapabili
 	if len(errs) > 0 {
 		err = util.JoinErrs(errs)
 		return nil, http.StatusInternalServerError, nil, fmt.Errorf(
-			"query exception: could not build api_capbility query with params: %v, error: %v",
+			"query exception: could not build api_capability query with params: %v, error: %v",
 			params,
 			err,
 		)
@@ -76,16 +76,14 @@ func getAPICapabilities(tx *sqlx.Tx, params map[string]string) ([]tc.APICapabili
 
 	query := selectQuery + where + orderBy + pagination
 	rows, err := tx.NamedQuery(query, queryValues)
+
 	if err != nil {
-		return nil, http.StatusInternalServerError, nil, fmt.Errorf(
-			"db exception: could not execute api_capbility query with params: %v, error: %v",
-			params,
-			err,
-		)
+		usrErr, sysErr, errCode := api.ParseDBError(err)
+		return nil, errCode, usrErr, sysErr
 	}
 	defer rows.Close()
 
-	var apiCaps []tc.APICapability
+	apiCaps := []tc.APICapability{}
 	for rows.Next() {
 		var ac tc.APICapability
 		err = rows.Scan(