rob05c commented on a change in pull request #2849: TO Golang: Updated
ParseDBError
URL: https://github.com/apache/trafficcontrol/pull/2849#discussion_r219261761
##########
File path: traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go
##########
@@ -101,24 +103,45 @@ func parseCriteriaAndQueryValues(queryParamsToSQLCols
map[string]WhereColumnInfo
return criteria, queryValues, errs
}
-//parses pq errors for uniqueness constraint violations
-func ParsePQUniqueConstraintError(err *pq.Error) (error, tc.ApiErrorType) {
- 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(column + " " + dupValue + "
already exists."), tc.DataConflictError
- }
+// small helper function to help with parsing below
+func toCamelCase(str string) string {
Review comment:
Would you mind writing a unit test for this? It's a small, pure function, an
excellent candidate for a unit test to verify it behaves correctly with good
and bad input. Especially since it's doing array accesses, which can easily
cause a nil panic.
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services