@@ -522,7 +522,7 @@ func setAuthorizedClusters(ctx context.Context, db *sql.DB, parsed *queryOptions
522
522
continue
523
523
}
524
524
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 ) ... )
526
526
527
527
return parsed , err
528
528
}
@@ -720,7 +720,7 @@ func getSingleComplianceEvent(db *sql.DB, w http.ResponseWriter,
720
720
return
721
721
}
722
722
723
- log .Error (err , "Failed to unmarshal the database results" )
723
+ log .Error (err , "Failed to unmarshal the database results" , getPqErrKeyVals ( err ) ... )
724
724
writeErrMsgJSON (w , "Internal Error" , http .StatusInternalServerError )
725
725
726
726
return
@@ -755,6 +755,26 @@ func getSingleComplianceEvent(db *sql.DB, w http.ResponseWriter,
755
755
}
756
756
}
757
757
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
+
758
778
func getClusterNameFromID (ctx context.Context , db * sql.DB , clusterID string ) (name string , err error ) {
759
779
err = db .QueryRowContext (ctx ,
760
780
`SELECT name FROM clusters WHERE cluster_id = $1` , clusterID ,
@@ -944,7 +964,7 @@ LEFT JOIN policies ON compliance_events.policy_id = policies.id` + whereClause /
944
964
var total uint64
945
965
946
966
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 ) ... )
948
968
writeErrMsgJSON (w , "Internal Error" , http .StatusInternalServerError )
949
969
950
970
return
@@ -1025,7 +1045,7 @@ func postComplianceEvent(serverContext *ComplianceServerCtx, cfg *rest.Config, w
1025
1045
1026
1046
clusterFK , err := GetClusterForeignKey (r .Context (), serverContext .DB , reqEvent .Cluster )
1027
1047
if err != nil {
1028
- log .Error (err , "error getting cluster foreign key" )
1048
+ log .Error (err , "error getting cluster foreign key" , getPqErrKeyVals ( err ) ... )
1029
1049
writeErrMsgJSON (w , "Internal Error" , http .StatusInternalServerError )
1030
1050
1031
1051
return
@@ -1036,7 +1056,7 @@ func postComplianceEvent(serverContext *ComplianceServerCtx, cfg *rest.Config, w
1036
1056
if reqEvent .ParentPolicy != nil {
1037
1057
pfk , err := getParentPolicyForeignKey (r .Context (), serverContext , * reqEvent .ParentPolicy )
1038
1058
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 ) ... )
1040
1060
writeErrMsgJSON (w , "Internal Error" , http .StatusInternalServerError )
1041
1061
1042
1062
return
@@ -1047,7 +1067,7 @@ func postComplianceEvent(serverContext *ComplianceServerCtx, cfg *rest.Config, w
1047
1067
1048
1068
policyFK , err := getPolicyForeignKey (r .Context (), serverContext , reqEvent .Policy )
1049
1069
if err != nil {
1050
- log .Error (err , "error getting policy foreign key" )
1070
+ log .Error (err , "error getting policy foreign key" , getPqErrKeyVals ( err ) ... )
1051
1071
writeErrMsgJSON (w , "Internal Error" , http .StatusInternalServerError )
1052
1072
1053
1073
return
@@ -1083,7 +1103,7 @@ func postComplianceEvent(serverContext *ComplianceServerCtx, cfg *rest.Config, w
1083
1103
serverContext .Lock .Unlock ()
1084
1104
serverContext .Lock .RLock ()
1085
1105
} else {
1086
- log .Error (err , "error inserting compliance event" )
1106
+ log .Error (err , "error inserting compliance event" , getPqErrKeyVals ( err ) ... )
1087
1107
}
1088
1108
1089
1109
writeErrMsgJSON (w , "Internal Error" , http .StatusInternalServerError )
0 commit comments