WIP: Respond with 409 when resources conflict
Created by: dblessing
** Work in progress ** Seeking feedback
Per the API docs, a 409 response should be sent when resources conflict. Right now only one part of the API actually implements this functionality (teams I believe). I believe we should make this work across the API where applicable. This is my first crack at a solution. I am seeking feedback on whether you feel this is a viable solution. If so, I will implement across the API.
Steps to reproduce: Using the API, attempt to create a project with a name or path that already exists.
Expected behavior: This should result in a 409 conflict response.
Observed behavior: A 404 not found response is received.
Errors that arise from conflicting resources are actually sent by ActiveRecord. It sends a hash that looks something like { 'name' => 'has already been taken' } or { 'path' => 'has already been taken' }. So if we check the value for 'has already been taken' we know there's a resource conflict. Then, the key of the has (like 'name') represents what attribute the error is for. Documentation for ActiveRecord validations is at http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html
Thanks for your feedback.
85 85 if @project.errors[:limit_reached].present? 86 86 error!(@project.errors[:limit_reached], 403) 87 87 end 88 handle_activerecord_errors(@project.errors) 85 85 if @project.errors[:limit_reached].present? 86 86 error!(@project.errors[:limit_reached], 403) 87 87 end 88 handle_activerecord_errors(@project.errors) Created by: dblessing
what do you mean by the first part? From looking at error handling in general it seems that once an API error is thrown it stops. Even without this addition the errors are stacked - first
limit_reached
followed bynot_found!
I can confirm that a 409 is the only response generated by this code in a conflict scenario so only 1 error is getting evaluated.
By Administrator on 2013-06-14T16:38:06 (imported from GitLab project)
60 60 attrs 61 61 end 62 62 63 def handle_activerecord_errors(errors) 64 attribute, message = errors.first Created by: dblessing
If there are multiple errors, how should it be handled? Currently, it is not handled. For example on project creation, it checks for
limit_reached
or throwsnot_found!
. If there are multiple errors of the same type we can put them all together in one message, but it gets crazy when we have different types. Open to suggestions...By Administrator on 2013-06-14T16:40:01 (imported from GitLab project)