Skip to content

Commit 3ef3aea

Browse files
committed
Handle when error is not a pqerror
``` panic: interface conversion: error is *net.OpError, not *pq.Error [recovered] panic: interface conversion: error is *net.OpError, not *pq.Error ```
1 parent 8412cc3 commit 3ef3aea

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

pkg/postgres/role.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,30 @@ const (
1919
REASIGN_OBJECTS = `REASSIGN OWNED BY "%s" TO "%s"`
2020
)
2121

22+
func isPQError(err error, codes ...pq.ErrorCode) bool {
23+
if err == nil {
24+
return false
25+
}
26+
27+
pgError, ok := err.(*pq.Error)
28+
if ok {
29+
if len(codes) == 0 {
30+
// just checking if its a pgerror
31+
return true
32+
}
33+
for _, code := range codes {
34+
if pgError.Code == code {
35+
return true
36+
}
37+
}
38+
}
39+
return false
40+
}
41+
2242
func (c *pg) CreateGroupRole(role string) error {
2343
// Error code 42710 is duplicate_object (role already exists)
2444
_, err := c.db.Exec(fmt.Sprintf(CREATE_GROUP_ROLE, role))
25-
if err != nil && err.(*pq.Error).Code != "42710" {
45+
if err != nil && isPQError(err, "42710") {
2646
return err
2747
}
2848
return nil
@@ -64,7 +84,7 @@ func (c *pg) DropRole(role, newOwner, database string, logger logr.Logger) error
6484
// REASSIGN OWNED BY only works if the correct database is selected
6585
tmpDb, err := GetConnection(c.user, c.pass, c.host, c.port, database, c.args, logger)
6686
if err != nil {
67-
if err.(*pq.Error).Code == "3D000" {
87+
if isPQError(err, "3D000") {
6888
return nil // Database is does not exist (anymore)
6989
} else {
7090
return err
@@ -73,20 +93,20 @@ func (c *pg) DropRole(role, newOwner, database string, logger logr.Logger) error
7393
_, err = tmpDb.Exec(fmt.Sprintf(REASIGN_OBJECTS, role, newOwner))
7494
defer tmpDb.Close()
7595
// Check if error exists and if different from "ROLE NOT FOUND" => 42704
76-
if err != nil && err.(*pq.Error).Code != "42704" {
96+
if err != nil && isPQError(err, "42704") {
7797
return err
7898
}
7999

80100
// We previously assigned all objects to the operator's role so DROP OWNED BY will drop privileges of role
81101
_, err = tmpDb.Exec(fmt.Sprintf(DROP_OWNED_BY, role))
82102
// Check if error exists and if different from "ROLE NOT FOUND" => 42704
83-
if err != nil && err.(*pq.Error).Code != "42704" {
103+
if err != nil && isPQError(err, "42704") {
84104
return err
85105
}
86106

87107
_, err = c.db.Exec(fmt.Sprintf(DROP_ROLE, role))
88108
// Check if error exists and if different from "ROLE NOT FOUND" => 42704
89-
if err != nil && err.(*pq.Error).Code != "42704" {
109+
if err != nil && isPQError(err, "42704") {
90110
return err
91111
}
92112
return nil

pkg/postgres/role_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package postgres
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/lib/pq"
8+
)
9+
10+
func TestIsPQError(t *testing.T) {
11+
genericError := errors.New("regular error")
12+
13+
pqError := new(pq.Error)
14+
pqError.Code = "42710"
15+
16+
if isPQError(genericError, "42710") {
17+
t.Fatalf("generic error is not an pq error")
18+
}
19+
20+
if isPQError(pqError, "111111") {
21+
t.Fatalf("might be a pq error, but isn't this specific one")
22+
}
23+
24+
if !isPQError(pqError, "42710") {
25+
t.Fatalf("should match targeted pqerror")
26+
}
27+
28+
// multiple
29+
if !isPQError(pqError, "42710", "111111") {
30+
t.Fatalf("should match targeted pqerror")
31+
}
32+
}

0 commit comments

Comments
 (0)