Skip to content

Instantly share code, notes, and snippets.

@907th
Created June 28, 2016 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 907th/ad397c0c4a3a97d2436ee343c39a245f to your computer and use it in GitHub Desktop.
Save 907th/ad397c0c4a3a97d2436ee343c39a245f to your computer and use it in GitHub Desktop.
Exception helpers
module MemberApi
module ExceptionHelpers
# Usage:
# render_validation_errors(model) unless model.valid?
#
# @param model [ActiveRecord::Base]
#
def render_validation_errors(model, status: :bad_request)
render json: {
full_messages: model.errors.full_messages,
invalid_attributes: model.errors.to_h
}, status: status
end
# Usage:
# begin
# # ...
# rescue SomeError => error
# render_error error
# end
#
# @param error [StandardError]
#
def render_error(error, status: :internal_server_error)
render json: { error: error.message }, status: status
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment