You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@casbin.apache.org by hs...@apache.org on 2026/08/06 13:11:01 UTC
(casbin-lua-casbin) branch master updated: fix: propagate wrapped adapter errors in FilteredAdapter
This is an automated email from the ASF dual-hosted git repository.
hsluoyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/casbin-lua-casbin.git
The following commit(s) were added to refs/heads/master by this push:
new c0d7d1e fix: propagate wrapped adapter errors in FilteredAdapter
c0d7d1e is described below
commit c0d7d1edf3546c094d7db60c1c3bd2465d227f53
Author: Yang Luo <hs...@gmail.com>
AuthorDate: Thu Aug 6 21:10:54 2026 +0800
fix: propagate wrapped adapter errors in FilteredAdapter
---
src/persist/file_adapter/FilteredAdapter.lua | 35 +++++++++++++++++++++-------
tests/persist/adapter_error_spec.lua | 29 +++++++++++++++++++++++
2 files changed, 56 insertions(+), 8 deletions(-)
diff --git a/src/persist/file_adapter/FilteredAdapter.lua b/src/persist/file_adapter/FilteredAdapter.lua
index 0020d5c..04ecd28 100644
--- a/src/persist/file_adapter/FilteredAdapter.lua
+++ b/src/persist/file_adapter/FilteredAdapter.lua
@@ -27,6 +27,15 @@ local FilteredAdapter = {
}
setmetatable(FilteredAdapter, Adapter)
+--[[
+ * hasFailed tells whether a wrapped adapter reported a failure through the
+ * "return false/nil, err" convention. FilteredAdapter only delegates, so it
+ * must hand that failure back to the enforcer instead of dropping it.
+]]
+local function hasFailed(ok, err)
+ return ok == false or (ok == nil and err ~= nil)
+end
+
function FilteredAdapter:new(filePath)
local o = {}
setmetatable(o, self)
@@ -49,9 +58,12 @@ function FilteredAdapter:loadFilteredPolicy(model, filter)
end
if filter == nil then
- self.adapter:loadPolicy(model)
+ local ok, err = self.adapter:loadPolicy(model)
+ if hasFailed(ok, err) then
+ return false, err
+ end
self.isFiltered = false
- return
+ return true
end
if not filter.G or not filter.P then
@@ -60,6 +72,8 @@ function FilteredAdapter:loadFilteredPolicy(model, filter)
self:loadFilteredPolicyFile(model, filter)
self.isFiltered = true
end
+
+ return true
end
--[[
@@ -140,36 +154,41 @@ end
* loadPolicy loads all policy rules from the storage.
]]
function FilteredAdapter:loadPolicy(model)
- self.adapter:loadPolicy(model)
+ local ok, err = self.adapter:loadPolicy(model)
+ if hasFailed(ok, err) then
+ return false, err
+ end
+
self.isFiltered = false
+ return true
end
--[[
* savePolicy saves all policy rules to the storage.
]]
function FilteredAdapter:savePolicy(model)
- self.adapter:savePolicy(model)
+ return self.adapter:savePolicy(model)
end
--[[
* addPolicy adds a policy rule to the storage.
]]
function FilteredAdapter:addPolicy(sec, ptype, rule)
- self.adapter:addPolicy(sec, ptype, rule)
+ return self.adapter:addPolicy(sec, ptype, rule)
end
--[[
* removePolicy removes a policy rule from the storage.
]]
function FilteredAdapter:removePolicy(sec, ptype, rule)
- self.adapter:removePolicy(sec, ptype, rule)
+ return self.adapter:removePolicy(sec, ptype, rule)
end
--[[
* removeFilteredPolicy removes policy rules that match the filter from the storage.
]]
-function FilteredAdapter:removeFilteredPolicyPolicy(sec, ptype, fieldIndex, fieldValues)
- self.adapter:removeFilteredPolicyPolicy(sec, ptype, fieldIndex, fieldValues)
+function FilteredAdapter:removeFilteredPolicy(sec, ptype, fieldIndex, fieldValues)
+ return self.adapter:removeFilteredPolicy(sec, ptype, fieldIndex, fieldValues)
end
return FilteredAdapter
\ No newline at end of file
diff --git a/tests/persist/adapter_error_spec.lua b/tests/persist/adapter_error_spec.lua
index 5f6e717..839788f 100644
--- a/tests/persist/adapter_error_spec.lua
+++ b/tests/persist/adapter_error_spec.lua
@@ -14,6 +14,7 @@
local Adapter = require("src.persist.Adapter")
local Enforcer = require("src.main.Enforcer")
+local FilteredAdapter = require("src.persist.file_adapter.FilteredAdapter")
local path = os.getenv("PWD") or io.popen("cd"):read()
local model_path = path .. "/examples/basic_model.conf"
@@ -101,4 +102,32 @@ describe("adapter error tests", function ()
assert.is.True(e:enforce("alice", "data1", "read"))
end)
+
+ -- FilteredAdapter only delegates, so it must not swallow the wrapped
+ -- adapter's failure on its way back to the enforcer.
+ it("test filtered adapter propagates the wrapped adapter error", function ()
+ local fa = FilteredAdapter:new(policy_path)
+ fa.adapter = FailingAdapter({loadPolicy = true, savePolicy = true})
+
+ local ok, err = fa:loadPolicy({})
+ assert.is.False(ok)
+ assert.are.equals("connection to the database failed", err)
+
+ ok, err = fa:loadFilteredPolicy({}, nil)
+ assert.is.False(ok)
+ assert.are.equals("connection to the database failed", err)
+
+ ok, err = fa:savePolicy({})
+ assert.is.False(ok)
+ assert.are.equals("the database is read only", err)
+ end)
+
+ it("test filtered adapter reports success of the wrapped adapter", function ()
+ local fa = FilteredAdapter:new(policy_path)
+ local e = Enforcer:new(model_path, fa)
+
+ e:loadFilteredPolicy(nil)
+
+ assert.is.True(e:enforce("alice", "data1", "read"))
+ end)
end)
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org
For additional commands, e-mail: commits-help@casbin.apache.org