Skip to content

Instantly share code, notes, and snippets.

@tobiashm
Created January 11, 2016 11:53
Show Gist options
  • Select an option

  • Save tobiashm/b039714d5cb95ddd4331 to your computer and use it in GitHub Desktop.

Select an option

Save tobiashm/b039714d5cb95ddd4331 to your computer and use it in GitHub Desktop.
Render error for JSON API
def render_error(status, error = nil)
status = Rack::Utils.status_code(status)
body = {
error: status,
reason: error.is_a?(Exception) ? error.to_s : Rack::Utils::HTTP_STATUS_CODES[status]
}
body[:detail] = error.backtrace.join("\n") if error.is_a?(Exception)
render status: status, json: body
end
# render_error :not_found
# rescue => e
# render_error 500, e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment