Comments (7)
I once tried to call error returned functions and panic if error encountered. But I found an issue: the call stack is lost. When an internal panic occurs, it usually carries with the call stack when the panic occurs. But if it is turned into an error, and panic the error. Then call stack will only be the code line that called panic(err)
. The original faulty call stack is lost. This is inconvenient for debugging errors.
If we make error returning functions the regular ones, some APIs have to be broken: Enforce()
has to return bool, error
instead of bool
. And the caller has to write: result, err := e.Enforce(xx, xx, xx)
instead of simple if e.Enforce(xx, xx, xx)
. I think this will complicate the usage? As far as I know, although the safe functions are already provided, no project in GitHub ever uses them. It seems that most people don't like to use complicated things?
from casbin.
Using panic might be useful for debugging, but I'm pretty sure I would never use panics in a live application. It takes the flow control from you. I know Go's error handling is quite unusal, but using panics is not a good idea either IMO.
from casbin.
OK. Can you make a PR about it?
from casbin.
Many would say that Panic should only be used when the service is unrecoverable and thus should go down.
It's also a way to indicate that something impossible has happened ...
This is only an example but real library functions should avoid panic...
One possible counterexample is during initialization: if the library truly cannot set itself up, it might be reasonable to panic...
https://golang.org/doc/effective_go.html#panic
See also:
[1]: https://go-proverbs.github.io/
[2]: https://github.com/golang/go/wiki/CodeReviewComments#dont-panic
from casbin.
@sagikazarmark @MaerF0x0 I have a new idea. We can return errors and panic at the same time. We set a flag named triggerPanic
. Then write code like:
err := xxx
if triggerPanic {
panic(err)
}
return err
Then I will make triggerPanic
to be false by default. So you can get errors as you want. And I can call enforcer.triggerPanic = true
and enjoy the panic when something crashes in the test scripts.
from casbin.
There are libraries which append the call stack to the error, i.e. Github.com/pkg/errors
from casbin.
It has been 2 years and Casbin v2.x has supported returning errors instead of causing panic. So no safe functions required any more. Closed.
https://github.com/casbin/casbin/releases/tag/v2.0.0
from casbin.
Related Issues (20)
- [Question] How to delete role in domain HOT 2
- [Question] HOT 2
- [Question] Why doesn't DeleteRole delete role completely from the policies? HOT 1
- [Question] Why CachedEnforcer uses sync.RWMutex just like sync.Mutex? HOT 6
- [Question] Is it expected that keyMatch3 handles `*` pattern? HOT 3
- [Bug]`GetRolesForUser("userId")` will raise nil error when using RBAC with conditions. HOT 5
- [Bug] nil pointer panic when calling role related functions with no role definition model HOT 2
- [Question] Implement row-level and column-level authorization for data in DB HOT 3
- [Feature] improve code quality by involve strict static check HOT 1
- [Question] Establish rules of users of different organizations visibility HOT 5
- [Question] Effects are ignored for any policy/request other than the first one HOT 3
- How can I design a matcher that looks for membership in two (g & g2) role_definitions? HOT 3
- casbin casbin to save or delete rules occurrence Can't call commit when autocommit=true exception HOT 2
- [Bug] Running Enforce and LoadPolicy concurrently can lead to cached errors in role-user relationships HOT 1
- [Question]update user roles failed HOT 4
- [Question] get matched role from user? HOT 2
- [Bug] keyMatch3 gives false positive to malformed expression in Golang casbin library HOT 2
- [Bug] RBAC Pattern not working with Conditions HOT 1
- [Question] Why is the permission check inconsistent HOT 5
- [Bug] Why breaking changes are introduced in minor releases? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from casbin.