Skip to content

Commit 83e3403

Browse files
panic handling (#1992)
1 parent 2d02ca5 commit 83e3403

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

pkg/user/casbin/Adapter.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func Create() *casbin.Enforcer {
6969
}
7070

7171
func AddPolicy(policies []Policy) []Policy {
72+
defer handlePanic()
7273
LoadPolicy()
7374
var failed = []Policy{}
7475
for _, p := range policies {
@@ -100,6 +101,7 @@ func AddPolicy(policies []Policy) []Policy {
100101
}
101102

102103
func LoadPolicy() {
104+
defer handlePanic()
103105
err := e.LoadPolicy()
104106
if err != nil {
105107
fmt.Println("error in reloading policies", err)
@@ -109,6 +111,7 @@ func LoadPolicy() {
109111
}
110112

111113
func RemovePolicy(policies []Policy) []Policy {
114+
defer handlePanic()
112115
var failed = []Policy{}
113116
for _, p := range policies {
114117
success := false
@@ -146,7 +149,13 @@ func GetUserByRole(role string) ([]string, error) {
146149
return e.GetUsersForRole(role)
147150
}
148151

149-
func RemovePoliciesByRoles(roles string) bool{
152+
func RemovePoliciesByRoles(roles string) bool {
150153
roles = strings.ToLower(roles)
151154
return e.RemovePolicy([]string{roles})
152-
}
155+
}
156+
157+
func handlePanic() {
158+
if err := recover(); err != nil {
159+
log.Println("panic occurred:", err)
160+
}
161+
}

pkg/user/casbin/rbac.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ func (e *EnforcerImpl) enforce(enf *casbin.Enforcer, rvals ...interface{}) bool
100100
email = "admin"
101101
}
102102
rvals[0] = strings.ToLower(email)
103-
return enf.Enforce(rvals...)
103+
defer handlePanic()
104+
enforcedStatus := enf.Enforce(rvals...)
105+
return enforcedStatus
104106
}
105107

106108
// enforce is a helper to additionally check a default role and invoke a custom claims enforcement function
@@ -109,7 +111,9 @@ func (e *EnforcerImpl) enforceByEmail(enf *casbin.Enforcer, rvals ...interface{}
109111
if len(rvals) == 0 {
110112
return false
111113
}
112-
return enf.Enforce(rvals...)
114+
defer handlePanic()
115+
enforcedStatus := enf.Enforce(rvals...)
116+
return enforcedStatus
113117
}
114118

115119
// MatchKeyByPartFunc is the wrapper of our own customised MatchKeyByPart Func

0 commit comments

Comments
 (0)