mirror of
https://github.com/discourse/discourse.git
synced 2024-11-24 12:03:35 +08:00
25 lines
606 B
Ruby
25 lines
606 B
Ruby
module JsonError
|
|
|
|
def create_errors_json(obj)
|
|
|
|
# If we're passed a string, assume that is the error message
|
|
return {errors: [obj]} if obj.is_a?(String)
|
|
|
|
# If it's an AR exception target the record
|
|
obj = obj.record if obj.is_a?(ActiveRecord::RecordInvalid)
|
|
|
|
# If it looks like an activerecord object, extract its messages
|
|
return {errors: obj.errors.full_messages } if obj.respond_to?(:errors) && obj.errors.present?
|
|
|
|
# default to a generic error
|
|
JsonError.generic_error
|
|
end
|
|
|
|
private
|
|
|
|
def self.generic_error
|
|
{errors: [I18n.t('js.generic_error')]}
|
|
end
|
|
|
|
end
|