From 998cd3cb63d56a0058c8e519d7c20e3d6e540899 Mon Sep 17 00:00:00 2001 From: jubianchi Date: Mon, 18 Aug 2014 20:09:09 +0200 Subject: Improve error reporting on users API * users (#6878, #3526, #4209): Validation error messages are now exposed through 400 responses, 409 response are sent in case of duplicate email or username * MRs (#5335): 409 responses are sent in case of duplicate merge request (source/target branches), 422 responses are sent when submiting MR fo/from unrelated forks * issues * labels * projects --- lib/api/labels.rb | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) (limited to 'lib/api/labels.rb') diff --git a/lib/api/labels.rb b/lib/api/labels.rb index 2fdf53ffec2..78ca58ad0d1 100644 --- a/lib/api/labels.rb +++ b/lib/api/labels.rb @@ -30,16 +30,14 @@ module API attrs = attributes_for_keys [:name, :color] label = user_project.find_label(attrs[:name]) - if label - return render_api_error!('Label already exists', 409) - end + conflict!('Label already exists') if label label = user_project.labels.create(attrs) if label.valid? present label, with: Entities::Label else - render_api_error!(label.errors.full_messages.join(', '), 400) + render_validation_error!(label) end end @@ -56,9 +54,7 @@ module API required_attributes! [:name] label = user_project.find_label(params[:name]) - if !label - return render_api_error!('Label not found', 404) - end + not_found!('Label') unless label label.destroy end @@ -66,10 +62,11 @@ module API # Updates an existing label. At least one optional parameter is required. # # Parameters: - # id (required) - The ID of a project - # name (optional) - The name of the label to be deleted - # color (optional) - Color of the label given in 6-digit hex - # notation with leading '#' sign (e.g. #FFAABB) + # id (required) - The ID of a project + # name (required) - The name of the label to be deleted + # new_name (optional) - The new name of the label + # color (optional) - Color of the label given in 6-digit hex + # notation with leading '#' sign (e.g. #FFAABB) # Example Request: # PUT /projects/:id/labels put ':id/labels' do @@ -77,16 +74,14 @@ module API required_attributes! [:name] label = user_project.find_label(params[:name]) - if !label - return render_api_error!('Label not found', 404) - end + not_found!('Label not found') unless label attrs = attributes_for_keys [:new_name, :color] if attrs.empty? - return render_api_error!('Required parameters "name" or "color" ' \ - 'missing', - 400) + render_api_error!('Required parameters "new_name" or "color" ' \ + 'missing', + 400) end # Rename new name to the actual label attribute name @@ -95,7 +90,7 @@ module API if label.update(attrs) present label, with: Entities::Label else - render_api_error!(label.errors.full_messages.join(', '), 400) + render_validation_error!(label) end end end -- cgit v1.2.1