mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 09:13:38 +08:00
30990006a9
This reduces chances of errors where consumers of strings mutate inputs and reduces memory usage of the app. Test suite passes now, but there may be some stuff left, so we will run a few sites on a branch prior to merging
44 lines
874 B
Ruby
44 lines
874 B
Ruby
# frozen_string_literal: true
|
|
|
|
# Helper functions for dealing with errors and objects that have
|
|
# child objects with errors
|
|
module HasErrors
|
|
attr_reader :errors
|
|
attr_accessor :forbidden, :not_found, :conflict
|
|
|
|
def errors
|
|
@errors ||= ActiveModel::Errors.new(self)
|
|
end
|
|
|
|
def validate_child(obj)
|
|
return true if obj.valid?
|
|
add_errors_from(obj)
|
|
false
|
|
end
|
|
|
|
def rollback_with!(obj, error)
|
|
obj.errors.add(:base, error)
|
|
rollback_from_errors!(obj)
|
|
end
|
|
|
|
def rollback_from_errors!(obj)
|
|
add_errors_from(obj)
|
|
raise ActiveRecord::Rollback.new
|
|
end
|
|
|
|
def add_error(msg)
|
|
errors.add(:base, msg) unless errors[:base].include?(msg)
|
|
end
|
|
|
|
def add_errors_from(obj)
|
|
return if obj.blank?
|
|
|
|
if obj.is_a?(StandardError)
|
|
return add_error(obj.message)
|
|
end
|
|
|
|
obj.errors.full_messages.each { |msg| add_error(msg) }
|
|
end
|
|
|
|
end
|