Skip to content

Instantly share code, notes, and snippets.

@rubyrider
Created July 22, 2014 14:24
Show Gist options
  • Save rubyrider/0bdb1f5924dc4678e49c to your computer and use it in GitHub Desktop.
Save rubyrider/0bdb1f5924dc4678e49c to your computer and use it in GitHub Desktop.
# Expected
# "error": {
# "status": 422,
# "code": "some_object/validation",
# "message": "Cannot create the {some_object} because provided data is not valid",
# "details": "More thorough description of a problem and probably even solution for curious users",
# "href": "http://some.url/to-describe/the-problem/in-even-more-details/optional",
# "errors": {
# "name": {
# "code": "validation/missing_required",
# "message": "The name is blank."
# },
# "email": {
# "code": "validation/invalid_format",
# "message": "The email format is invalid."
# },
# "description": {
# "code": "validation/invalid_value/too_short",
# "message": "The description is too short. Please provide more details."
# }
# }
# }
#simplified version of error response handling
#this requires some work.. but an ok start
module ErrorResponseFormat
def error_response_wrapper(object, status = 422, options = {})
self.status = status
error_obj, code_types = ErrorResponseFormatter.new(object).show
code = ErrorCodeFormatter.new(code_types).show
{
status: status,
code: code,
message: "Cannot add #{object.class.to_s.downcase} because some input is not correct",
href: request.url,
fieldErrors: error_obj
}
end
class ErrorCodeFormatter
def initialize(codes)
@codes = codes.flatten.uniq.join(' / ') if codes.is_a? Array
end
def show
@codes || ''
end
end
class ErrorResponseFormatter
def initialize(object)
@object = object
end
def show
errors_array = []
code_types = []
error_hash ||= {}
if @object.errors.is_a? ActiveModel::Errors
@object.errors.messages.each do |attribute, message|
error_hash[attribute] ||= {
message: message,
code: 'validations'
}
code_types << 'validations'
end
errors_array << error_hash
end
[errors_array, code_types]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment