Skip to content

Commit

Permalink
Pass class name to Sequel::MassAssignmentRestriction error
Browse files Browse the repository at this point in the history
If the instance has a class name, pass this name to the error
message to better understand the error in monitoring systems such as Sentry.
  • Loading branch information
artofhuman committed Sep 14, 2023
1 parent 92c84d4 commit 33e2558
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
11 changes: 8 additions & 3 deletions lib/sequel/model/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2035,15 +2035,20 @@ def set_restricted(hash, type)
if meths.include?(m)
set_column_value(m, v)
elsif strict
err_msg = -> (msg) do
msg << " for class #{self.class.name}" if self.class.name
msg
end

# Avoid using respond_to? or creating symbols from user input
if public_methods.map(&:to_s).include?(m)
if Array(model.primary_key).map(&:to_s).member?(k.to_s) && model.restrict_primary_key?
raise MassAssignmentRestriction, "#{k} is a restricted primary key"
raise MassAssignmentRestriction, err_msg.call("#{k} is a restricted primary key")
else
raise MassAssignmentRestriction, "#{k} is a restricted column"
raise MassAssignmentRestriction, err_msg.call("#{k} is a restricted column")
end
else
raise MassAssignmentRestriction, "method #{m} doesn't exist"
raise MassAssignmentRestriction, err_msg.call("method #{m} doesn't exist")
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/extensions/csv_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ def self.name; 'Album' end

it "should raise an error if attempting to set a restricted column and :all_columns is not used" do
@Artist.restrict_primary_key
proc{@Artist.from_csv(@artist.to_csv)}.must_raise(Sequel::MassAssignmentRestriction)
err = proc{@Artist.from_csv(@artist.to_csv)}.must_raise(Sequel::MassAssignmentRestriction)
err.message.must_equal("id is a restricted primary key for class Artist")
end

it "should use a dataset's selected columns" do
Expand Down
16 changes: 15 additions & 1 deletion spec/model/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -664,13 +664,27 @@ class ModelTest2 < Sequel::Model(@db[:foo])
end

it "should raise an error if a missing/restricted column/method is accessed" do
proc{@c.new(:a=>1)}.must_raise(Sequel::MassAssignmentRestriction)
err = proc{@c.new(:a=>1)}.must_raise(Sequel::MassAssignmentRestriction)
err.message.must_equal("method a= doesn't exist")
proc{@c.create(:a=>1)}.must_raise(Sequel::MassAssignmentRestriction)
c = @c.new
proc{c.set(:a=>1)}.must_raise(Sequel::MassAssignmentRestriction)
proc{c.update(:a=>1)}.must_raise(Sequel::MassAssignmentRestriction)
end

it "should add class name to error message" do
item = Class.new(Sequel::Model(:items)) do
columns :id
end

item.class_eval do
def self.name; 'Item' end
end

err = proc{item.new(:a=>1)}.must_raise(Sequel::MassAssignmentRestriction)
err.message.must_equal("method a= doesn't exist for class Item")
end

it "should be disabled by strict_param_setting = false" do
@c.strict_param_setting = false
@c.strict_param_setting.must_equal false
Expand Down

0 comments on commit 33e2558

Please sign in to comment.