Created
January 11, 2016 11:53
-
-
Save tobiashm/b039714d5cb95ddd4331 to your computer and use it in GitHub Desktop.
Render error for JSON API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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