-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Description
Sample code to reproduce issue
def grant_feature
ActiveRecord::Base.transaction do
some_role = Role.find(role_id)
AccessControl::SomePolicy.new(some_role).grant!(:some_action)
end
endException:
Could not grant action 1 on object AccessControl::SomePolicy with id 4 for actor #Role:0x00007fa8f1f5bad0 because PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block
This exception is raised here:
accessly/lib/accessly/permission/grant.rb
Line 75 in 1a477f9
| raise Accessly::GrantError.new("Could not grant action #{action_id} on object #{object_type} for actor #{@actor} because #{e}") |
But is occurring after
rescue ActiveRecord::RecordNotUnique
nil
is encountered which basically hides the actual error.
- Try to grant! new permission within transaction
- Permission record already exists which is rescued by rescue ActiveRecord::RecordNotUnique and returns nil
- Transaction then fails and returns the secondary exception message: "Could not grant action 1 on object AccessControl::SomePolicy with id 4"
Underlying issue
The problem is that we're encountering an exception within a transaction which fails the transaction but is being silenced by the return of nil
Potential options
- I know it is trying to avoid the lookup of db records here but something like an upsert could be a better option instead of rescuing ActiveRecord::RecordNotUnique and silencing the error
- We could first check to see if the record exists with a
find_byand then branch to create! if it doesn't or simply return nil without an rescuing a RecordNotUnique exception - We could use
find_or_create_by!kinda like upsert
Metadata
Metadata
Assignees
Labels
No labels