Browse Source

Improved error codes on model validation.

Martin Edenhofer 8 years ago
parent
commit
ec55c81302

+ 6 - 1
app/controllers/application_controller.rb

@@ -21,6 +21,8 @@ class ApplicationController < ActionController::Base
   rescue_from StandardError, with: :server_error
   rescue_from ExecJS::RuntimeError, with: :server_error
   rescue_from ActiveRecord::RecordNotFound, with: :not_found
+  rescue_from ActiveRecord::StatementInvalid, with: :unprocessable_entity
+  rescue_from ActiveRecord::RecordInvalid, with: :unprocessable_entity
   rescue_from ArgumentError, with: :unprocessable_entity
   rescue_from Exceptions::UnprocessableEntity, with: :unprocessable_entity
   rescue_from Exceptions::NotAuthorized, with: :unauthorized
@@ -378,7 +380,7 @@ class ApplicationController < ActionController::Base
     params.delete(:form_id)
 
     # check min. params
-    raise 'Need at least article: { body: "some text" }' if !params[:body]
+    raise Exceptions::UnprocessableEntity, 'Need at least article: { body: "some text" }' if !params[:body]
 
     # fill default values
     if params[:type_id].empty? && params[:type].empty?
@@ -639,6 +641,9 @@ class ApplicationController < ActionController::Base
     data = {
       error: error
     }
+    if error =~ /Validation failed: (.+?)(,|$)/i
+      data[:error_human] = $1
+    end
     if error =~ /(already exists|duplicate key|duplicate entry)/i
       data[:error_human] = 'Object already exists!'
     end

+ 9 - 9
app/models/application_model.rb

@@ -76,7 +76,7 @@ returns
     end
 
     if params.nil?
-      raise "No params for #{self}!"
+      raise ArgumentError, "No params for #{self}!"
     end
 
     data = {}
@@ -131,7 +131,7 @@ returns
 
         # complain if we found no reference
         if !lookup
-          raise "No value found for '#{assoc.name}' with id #{item_id.inspect}"
+          raise ArgumentError, "No value found for '#{assoc.name}' with id #{item_id.inspect}"
         end
         list.push item_id
       }
@@ -165,7 +165,7 @@ returns
 
         # complain if we found no reference
         if !lookup
-          raise "No lookup value found for '#{assoc.name}': #{value.inspect}"
+          raise ArgumentError, "No lookup value found for '#{assoc.name}': #{value.inspect}"
         end
         list.push lookup.id
       }
@@ -350,7 +350,7 @@ returns
               lookup = class_object.lookup(email: value)
             end
           else
-            raise "String is needed as ref value #{value.inspect} for '#{assoc.name}'"
+            raise ArgumentError, "String is needed as ref value #{value.inspect} for '#{assoc.name}'"
           end
         else
           lookup = class_object.lookup(name: value)
@@ -358,7 +358,7 @@ returns
 
         # complain if we found no reference
         if !lookup
-          raise "No lookup value found for '#{assoc.name}': #{value.inspect}"
+          raise ArgumentError, "No lookup value found for '#{assoc.name}': #{value.inspect}"
         end
 
         # release data value
@@ -393,7 +393,7 @@ returns
               lookup = class_object.lookup(email: item)
             end
           else
-            raise "String is needed in array ref as ref value #{value.inspect} for '#{assoc.name}'"
+            raise ArgumentError, "String is needed in array ref as ref value #{value.inspect} for '#{assoc.name}'"
           end
         else
           lookup = class_object.lookup(name: item)
@@ -401,7 +401,7 @@ returns
 
         # complain if we found no reference
         if !lookup
-          raise "No lookup value found for '#{assoc.name}': #{item.inspect}"
+          raise ArgumentError, "No lookup value found for '#{assoc.name}': #{item.inspect}"
         end
         lookup_ids.push lookup.id
       }
@@ -626,7 +626,7 @@ returns
       return
     end
 
-    raise 'Need name, id, login or email for lookup()'
+    raise ArgumentError, 'Need name, id, login or email for lookup()'
   end
 
 =begin
@@ -801,7 +801,7 @@ returns
       record.save
       return record
     else
-      raise 'Need name, login, email or locale for create_or_update()'
+      raise ArgumentError, 'Need name, login, email or locale for create_or_update()'
     end
   end
 

+ 4 - 0
app/models/ticket.rb

@@ -20,6 +20,10 @@ class Ticket < ApplicationModel
   before_update   :check_defaults, :check_title, :reset_pending_time, :check_escalation_update
   before_destroy  :destroy_dependencies
 
+  validates :group_id, presence: true
+  validates :priority_id, presence: true
+  validates :state_id, presence: true
+
   notify_clients_support
 
   latest_change_support

+ 4 - 4
test/controllers/tickets_controller_test.rb

@@ -62,10 +62,10 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
       },
     }
     post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
-    assert_response(500)
+    assert_response(422)
     result = JSON.parse(@response.body)
     assert_equal(Hash, result.class)
-    assert_equal('Attribute \'group_id\' required!', result['error_human'])
+    assert_equal('Group can\'t be blank', result['error_human'])
   end
 
   test '01.02 ticket create with agent - wrong group' do
@@ -81,7 +81,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
       },
     }
     post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
-    assert_response(500)
+    assert_response(422)
     result = JSON.parse(@response.body)
     assert_equal(Hash, result.class)
     assert_equal('No lookup value found for \'group\': "not_existing"', result['error'])
@@ -98,7 +98,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
       article: {},
     }
     post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
-    assert_response(500)
+    assert_response(422)
     result = JSON.parse(@response.body)
     assert_equal(Hash, result.class)
     assert_equal('Need at least article: { body: "some text" }', result['error'])