Skip to content
This repository
Browse code

Better controller error handling

  • Loading branch information...
commit 961c4c431510f85d29d2640bb5145eafcaf91250 1 parent a1cf283
Coraline Ada Ehmke authored December 07, 2012

Showing 1 changed file with 5 additions and 5 deletions. Show diff stats Hide diff stats

  1. 10  lib/faceted/controller.rb
10  lib/faceted/controller.rb
@@ -4,12 +4,12 @@ module Controller
4 4
 
5 5
     # For rendering a response with a single object, e.g.
6 6
     # render_response(@address)
7  
-    def render_response(obj)
  7
+    def render_response(obj, code=nil)
8 8
       render :json => {
9 9
         success:  obj.success,
10 10
         response: obj.to_hash,
11 11
         errors:   obj.errors
12  
-      }, :status => obj.success ? 200 : 400
  12
+      }, :status => code || obj.success ? 200 : 400
13 13
     end
14 14
 
15 15
     # For rendering a response with a multiple objects, e.g.
@@ -18,7 +18,7 @@ def render_response_with_collection(key, array)
18 18
       render :json => {
19 19
         success: true,
20 20
         response: {"#{key}".to_sym => array},
21  
-        errors:   nil
  21
+        errors: nil
22 22
       }
23 23
     end
24 24
 
@@ -28,8 +28,8 @@ def render_400(exception)
28 28
       render :json => {
29 29
         success: false,
30 30
         response: nil,
31  
-        errors: "Record not found: #{exception.message}"
32  
-      }, :status => 404
  31
+        errors: "#{exception.message}"
  32
+      }, :status => 400
33 33
     end
34 34
 
35 35
     # In your base API controller:

0 notes on commit 961c4c4

Please sign in to comment.
Something went wrong with that request. Please try again.