rob05c commented on a change in pull request #2849: TO Golang: Updated
ParseDBError
URL: https://github.com/apache/trafficcontrol/pull/2849#discussion_r219259434
##########
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:
All the existing usages of `ParsePQUniqueConstraintError` are already using
the new `userErr, sysErr, errCode` idiom. Since you're adding 2 more `ParsePQ`
funcs, would you mind changing all of them to return the idiom directly, so
`TypeErrToAPIErr` isn't necessary? It looks like there are only 4 other usages
----------------------------------------------------------------
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