Enabling already enabled deploy key from API causes "Internal Server Error"
Summary
When trying to enable deploy key that's already enabled for this project API returns {"message":"500 Internal Server Error"}
and HTTP status 500.
Steps to reproduce
- Add deploy key to project using https://docs.gitlab.com/ce/api/deploy_keys.html#add-deploy-key
- Enable the same key for same project using https://docs.gitlab.com/ce/api/deploy_keys.html#enable-a-deploy-key
Expected behavior
Returns Bad Request status or something like that.
Actual behavior
Returns 500 Internal Server Error.
Relevant logs and/or screenshots
Log:
Started POST "/api/v3/projects/5/deploy_keys/12/enable" for 127.0.0.1 at 2016-09-28 20:15:22 +0300
.....
ActiveRecord::RecordInvalid (Validation failed: Deploy key already exists in project):
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/validations.rb:79:in `raise_record_invalid'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/validations.rb:43:in `save!'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/transactions.rb:291:in `block in save!'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/transactions.rb:220:in `transaction'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/transactions.rb:291:in `save!'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/has_many_through_association.rb:117:in `save_through_record'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/has_many_through_association.rb:66:in `insert_record'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_association.rb:563:in `block (2 levels) in concat_records'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_association.rb:408:in `replace_on_target'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_association.rb:403:in `add_to_target'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_association.rb:562:in `block in concat_records'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_association.rb:560:in `each'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_association.rb:560:in `concat_records'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/has_many_association.rb:180:in `concat_records'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/has_many_through_association.rb:44:in `concat_records'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_association.rb:168:in `block in concat'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_association.rb:183:in `block in transaction'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/transactions.rb:220:in `transaction'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_association.rb:182:in `transaction'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_association.rb:168:in `concat'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/has_many_through_association.rb:38:in `concat'
/home/gsmetal/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_proxy.rb:970:in `<<'
/home/gsmetal/projects/gitlab/gitlab-development-kit/gitlab/app/services/projects/enable_deploy_key_service.rb:7:in `execute'
/home/gsmetal/projects/gitlab/gitlab-development-kit/gitlab/lib/api/deploy_keys.rb:80:in `block (3 levels) in <class:DeployKeys>'
....