Skip to content

Commit adb2ede

Browse files
mprahlopenshift-merge-bot[bot]
authored andcommitted
Use errors.As instead of manual type assertions
Signed-off-by: mprahl <mprahl@users.noreply.github.com>
1 parent 394307d commit adb2ede

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

controllers/complianceeventsapi/complianceeventsapi_controller.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,9 @@ func RecordLocalClusterComplianceEvent(
374374
// If it's a unique constraint violation, then the event is a duplicate and can be ignored. If it's a foreign
375375
// key violation, that means the database experienced data loss and the foreign key is invalid, so the
376376
// compliance event can't be recorded.
377-
if pqErr, ok := err.(*pq.Error); ok { //nolint: errorlint
377+
var pqErr *pq.Error
378+
379+
if errors.As(err, &pqErr) {
378380
if pqErr.Code == postgresUniqueViolationCode {
379381
return nil
380382
}

controllers/complianceeventsapi/server.go

+9-12
Original file line numberDiff line numberDiff line change
@@ -759,17 +759,13 @@ func getSingleComplianceEvent(db *sql.DB, w http.ResponseWriter,
759759
// as a convenience so that the keys don't need to be explicitly set to interface{} types when using the
760760
// `getPqErrKeyVals(err, "key1", "val1")...“ syntax.
761761
func getPqErrKeyVals(err error, additionalKeyVals ...interface{}) []interface{} {
762-
unwrappedErr := err
762+
var pqErr *pq.Error
763763

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)
764+
if errors.As(err, &pqErr) {
765+
return append(
766+
[]interface{}{"dbMessage", pqErr.Message, "dbDetail", pqErr.Detail, "dbCode", pqErr.Code},
767+
additionalKeyVals...,
768+
)
773769
}
774770

775771
return additionalKeyVals
@@ -1083,8 +1079,9 @@ func postComplianceEvent(serverContext *ComplianceServerCtx, cfg *rest.Config, w
10831079
return
10841080
}
10851081

1086-
pqErr, ok := err.(*pq.Error) //nolint:errorlint
1087-
if ok && pqErr.Code == postgresForeignKeyViolationCode {
1082+
var pqErr *pq.Error
1083+
1084+
if errors.As(err, &pqErr) && pqErr.Code == postgresForeignKeyViolationCode {
10881085
// This can only happen if the cache is out of date due to data loss in the database because if the
10891086
// database ID is provided, it is validated against the database.
10901087
log.Info(

0 commit comments

Comments
 (0)