You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2018/09/20 17:53:41 UTC

[GitHub] moltzaum commented on a change in pull request #2849: TO Golang: Updated ParseDBError

moltzaum commented on a change in pull request #2849: TO Golang: Updated ParseDBError
URL: https://github.com/apache/trafficcontrol/pull/2849#discussion_r219259858
 
 

 ##########
 File path: traffic_ops/traffic_ops_golang/api/api.go
 ##########
 @@ -418,24 +419,23 @@ func TypeErrToAPIErr(err error, errType tc.ApiErrorType) (error, error, int) {
 // ParseDBErr parses pq errors for uniqueness constraint violations, and returns the (userErr, sysErr, httpCode) format expected by the API helpers.
 // The dataType is the name of the API object, e.g. 'coordinate' or 'delivery service', used to construct the error string.
 func ParseDBErr(ierr error, dataType string) (error, error, int) {
+
 	err, ok := ierr.(*pq.Error)
 	if !ok {
 		return nil, errors.New("database returned non pq error: " + err.Error()), http.StatusInternalServerError
 	}
-	if len(err.Constraint) > 0 && len(err.Detail) > 0 { //we only want to continue parsing if it is a constraint error with details
-		detail := err.Detail
-		if strings.HasPrefix(detail, "Key ") && strings.HasSuffix(detail, " already exists.") { //we only want to continue parsing if it is a uniqueness constraint error
-			detail = strings.TrimPrefix(detail, "Key ")
-			detail = strings.TrimSuffix(detail, " already exists.")
-			//should look like "(column)=(dupe value)" at this point
-			details := strings.Split(detail, "=")
-			if len(details) == 2 {
-				column := strings.Trim(details[0], "()")
-				dupValue := strings.Trim(details[1], "()")
-				return errors.New("a " + dataType + " with " + column + " " + dupValue + " already exists."), nil, http.StatusBadRequest
-			}
-		}
+
+	if usrErr, sysErr, errCode := TypeErrToAPIErr(dbhelpers.ParsePQNotNullConstraintError(err)); errCode != http.StatusOK {
 
 Review comment:
   Sure. I was thinking about that anyway.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services