Permalink
Browse files

Better controller error handling

  • Loading branch information...
1 parent a1cf283 commit 961c4c431510f85d29d2640bb5145eafcaf91250 Bantik committed Dec 7, 2012
Showing with 5 additions and 5 deletions.
  1. +5 −5 lib/faceted/controller.rb
View
10 lib/faceted/controller.rb
@@ -4,12 +4,12 @@ module Controller
# For rendering a response with a single object, e.g.
# render_response(@address)
- def render_response(obj)
+ def render_response(obj, code=nil)
render :json => {
success: obj.success,
response: obj.to_hash,
errors: obj.errors
- }, :status => obj.success ? 200 : 400
+ }, :status => code || obj.success ? 200 : 400
end
# For rendering a response with a multiple objects, e.g.
@@ -18,7 +18,7 @@ def render_response_with_collection(key, array)
render :json => {
success: true,
response: {"#{key}".to_sym => array},
- errors: nil
+ errors: nil
}
end
@@ -28,8 +28,8 @@ def render_400(exception)
render :json => {
success: false,
response: nil,
- errors: "Record not found: #{exception.message}"
- }, :status => 404
+ errors: "#{exception.message}"
+ }, :status => 400
end
# In your base API controller:

0 comments on commit 961c4c4

Please sign in to comment.