Skip to content

Commit 394307d

Browse files
mprahlopenshift-merge-bot[bot]
authored andcommitted
Add additional log details for database errors
Relates: https://issues.redhat.com/browse/ACM-10180 Signed-off-by: mprahl <[email protected]>
1 parent f7a8975 commit 394307d

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

controllers/complianceeventsapi/server.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ func setAuthorizedClusters(ctx context.Context, db *sql.DB, parsed *queryOptions
522522
continue
523523
}
524524

525-
log.Error(err, "Failed to get cluster name from cluster ID", "ID", id)
525+
log.Error(err, "Failed to get cluster name from cluster ID", getPqErrKeyVals(err, "ID", id)...)
526526

527527
return parsed, err
528528
}
@@ -720,7 +720,7 @@ func getSingleComplianceEvent(db *sql.DB, w http.ResponseWriter,
720720
return
721721
}
722722

723-
log.Error(err, "Failed to unmarshal the database results")
723+
log.Error(err, "Failed to unmarshal the database results", getPqErrKeyVals(err)...)
724724
writeErrMsgJSON(w, "Internal Error", http.StatusInternalServerError)
725725

726726
return
@@ -755,6 +755,26 @@ func getSingleComplianceEvent(db *sql.DB, w http.ResponseWriter,
755755
}
756756
}
757757

758+
// getPqErrKeyVals is a helper to add additional database error details to a log message. additionalKeyVals is provided
759+
// as a convenience so that the keys don't need to be explicitly set to interface{} types when using the
760+
// `getPqErrKeyVals(err, "key1", "val1")...“ syntax.
761+
func getPqErrKeyVals(err error, additionalKeyVals ...interface{}) []interface{} {
762+
unwrappedErr := err
763+
764+
for unwrappedErr != nil {
765+
if pqErr, ok := unwrappedErr.(*pq.Error); ok { //nolint: errorlint
766+
return append(
767+
[]interface{}{"dbMessage", pqErr.Message, "dbDetail", pqErr.Detail, "dbCode", pqErr.Code},
768+
additionalKeyVals...,
769+
)
770+
}
771+
772+
unwrappedErr = errors.Unwrap(unwrappedErr)
773+
}
774+
775+
return additionalKeyVals
776+
}
777+
758778
func getClusterNameFromID(ctx context.Context, db *sql.DB, clusterID string) (name string, err error) {
759779
err = db.QueryRowContext(ctx,
760780
`SELECT name FROM clusters WHERE cluster_id = $1`, clusterID,
@@ -944,7 +964,7 @@ LEFT JOIN policies ON compliance_events.policy_id = policies.id` + whereClause /
944964
var total uint64
945965

946966
if err := row.Scan(&total); err != nil {
947-
log.Error(err, "Failed to get the count of compliance events")
967+
log.Error(err, "Failed to get the count of compliance events", getPqErrKeyVals(err)...)
948968
writeErrMsgJSON(w, "Internal Error", http.StatusInternalServerError)
949969

950970
return
@@ -1025,7 +1045,7 @@ func postComplianceEvent(serverContext *ComplianceServerCtx, cfg *rest.Config, w
10251045

10261046
clusterFK, err := GetClusterForeignKey(r.Context(), serverContext.DB, reqEvent.Cluster)
10271047
if err != nil {
1028-
log.Error(err, "error getting cluster foreign key")
1048+
log.Error(err, "error getting cluster foreign key", getPqErrKeyVals(err)...)
10291049
writeErrMsgJSON(w, "Internal Error", http.StatusInternalServerError)
10301050

10311051
return
@@ -1036,7 +1056,7 @@ func postComplianceEvent(serverContext *ComplianceServerCtx, cfg *rest.Config, w
10361056
if reqEvent.ParentPolicy != nil {
10371057
pfk, err := getParentPolicyForeignKey(r.Context(), serverContext, *reqEvent.ParentPolicy)
10381058
if err != nil {
1039-
log.Error(err, "error getting parent policy foreign key")
1059+
log.Error(err, "error getting parent policy foreign key", getPqErrKeyVals(err)...)
10401060
writeErrMsgJSON(w, "Internal Error", http.StatusInternalServerError)
10411061

10421062
return
@@ -1047,7 +1067,7 @@ func postComplianceEvent(serverContext *ComplianceServerCtx, cfg *rest.Config, w
10471067

10481068
policyFK, err := getPolicyForeignKey(r.Context(), serverContext, reqEvent.Policy)
10491069
if err != nil {
1050-
log.Error(err, "error getting policy foreign key")
1070+
log.Error(err, "error getting policy foreign key", getPqErrKeyVals(err)...)
10511071
writeErrMsgJSON(w, "Internal Error", http.StatusInternalServerError)
10521072

10531073
return
@@ -1083,7 +1103,7 @@ func postComplianceEvent(serverContext *ComplianceServerCtx, cfg *rest.Config, w
10831103
serverContext.Lock.Unlock()
10841104
serverContext.Lock.RLock()
10851105
} else {
1086-
log.Error(err, "error inserting compliance event")
1106+
log.Error(err, "error inserting compliance event", getPqErrKeyVals(err)...)
10871107
}
10881108

10891109
writeErrMsgJSON(w, "Internal Error", http.StatusInternalServerError)

controllers/complianceeventsapi/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (ce ComplianceEvent) Validate(ctx context.Context, serverContext *Complianc
8383

8484
err := row.Scan(&exists)
8585
if err != nil {
86-
log.Error(err, "Failed to query for the existence of the parent policy ID")
86+
log.Error(err, "Failed to query for the existence of the parent policy ID", getPqErrKeyVals(err)...)
8787

8888
return errors.New("failed to determine if parent_policy.id is valid")
8989
}
@@ -112,7 +112,7 @@ func (ce ComplianceEvent) Validate(ctx context.Context, serverContext *Complianc
112112

113113
err := row.Scan(&exists)
114114
if err != nil {
115-
log.Error(err, "Failed to query for the existence of the policy ID")
115+
log.Error(err, "Failed to query for the existence of the policy ID", getPqErrKeyVals(err)...)
116116

117117
return errors.New("failed to determine if policy.id is valid")
118118
}

0 commit comments

Comments
 (0)