Skip to content

Commit

Permalink
build msg inside exception
Browse files Browse the repository at this point in the history
  • Loading branch information
artofhuman committed Sep 15, 2023
1 parent 51de3cf commit fe16f0b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
11 changes: 3 additions & 8 deletions lib/sequel/model/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2035,20 +2035,15 @@ 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, err_msg.call("#{k} is a restricted primary key")
raise MassAssignmentRestriction.new("#{k} is a restricted primary key", self)
else
raise MassAssignmentRestriction, err_msg.call("#{k} is a restricted column")
raise MassAssignmentRestriction.new("#{k} is a restricted column", self)
end
else
raise MassAssignmentRestriction, err_msg.call("method #{m} doesn't exist")
raise MassAssignmentRestriction.new("method #{m} doesn't exist", self)
end
end
end
Expand Down
9 changes: 6 additions & 3 deletions lib/sequel/model/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ def initialize(message=nil, model=nil)
UndefinedAssociation = Class.new(Error)
).name

(
# Raised when a mass assignment method is called in strict mode with either a restricted column
# or a column without a setter method.
MassAssignmentRestriction = Class.new(Error)
).name
class MassAssignmentRestriction < Error
def initialize(msg, model=nil)
msg += " for class #{model.class.name}" if model.class.name
super(msg)
end
end

# Exception class raised when +raise_on_save_failure+ is set and validation fails
class ValidationFailed < Error
Expand Down

0 comments on commit fe16f0b

Please sign in to comment.