Skip to content

Commit

Permalink
Provide better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobm committed May 26, 2022
1 parent db59493 commit a97b0a1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/administrate/not_authorized_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ def initialize(action:, resource:)
@action = action
@resource = resource

super("not allowed to #{action.inspect} this #{resource.class}")
case resource
when Module, String, Symbol
super("Not allowed to perform #{action.inspect} on #{resource.inspect}")
else
super(
"Not allowed to perform #{action.inspect} on the given " +
resource.class.name
)
end
end
end
end
51 changes: 51 additions & 0 deletions spec/lib/administrate/not_authorized_error_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require "rails_helper"

describe Administrate::NotAuthorizedError do
context "when the resource is a class or module" do
it "produces a message mentioning it directly" do
error = described_class.new(
resource: Administrate,
action: "foo",
)
expect(error.message).to eq(
%{Not allowed to perform "foo" on Administrate}
)
end
end

context "when the resource is a string" do
it "produces a message mentioning it directly" do
error = described_class.new(
resource: "User",
action: "foo",
)
expect(error.message).to eq(%{Not allowed to perform "foo" on "User"})
end
end

context "when the resource is a symbol" do
it "produces a message mentioning it directly" do
error = described_class.new(
resource: :user,
action: "foo",
)
expect(error.message).to eq(%{Not allowed to perform "foo" on :user})
end
end

context "when the resource is something else" do
it "produces a message that refers to the class of the resource" do
class TestStuff; end

error = described_class.new(
resource: TestStuff.new,
action: "foo",
)
expect(error.message).to eq(
%{Not allowed to perform "foo" on the given TestStuff}
)
ensure
remove_constants :TestStuff
end
end
end

0 comments on commit a97b0a1

Please sign in to comment.