Improved error codes on model validation.

This commit is contained in:
Martin Edenhofer 2016-09-09 13:29:33 +02:00
parent eceb9889b1
commit ec55c81302
4 changed files with 23 additions and 14 deletions

View file

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

View file

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

View file

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

View file

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