API: extracted helper method to provide 400 bad request error with description
Extracted a method for 400 error (Bad request) and adjusted code accordingly. The name of the missing attribute is used to show which one was missing from the request. It is used to give an appropriate message in the json response.
This commit is contained in:
parent
54ab9bb6df
commit
6fc3263e15
6 changed files with 28 additions and 17 deletions
|
@ -55,6 +55,12 @@ module Gitlab
|
||||||
render_api_error!('403 Forbidden', 403)
|
render_api_error!('403 Forbidden', 403)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def bad_request!(attribute)
|
||||||
|
message = ["400 (Bad request)"]
|
||||||
|
message << "\"" + attribute.to_s + "\" not given"
|
||||||
|
render_api_error!(message.join(' '), 400)
|
||||||
|
end
|
||||||
|
|
||||||
def not_found!(resource = nil)
|
def not_found!(resource = nil)
|
||||||
message = ["404"]
|
message = ["404"]
|
||||||
message << resource if resource
|
message << resource if resource
|
||||||
|
|
|
@ -13,9 +13,9 @@ module Gitlab
|
||||||
#
|
#
|
||||||
def handle_merge_request_error(merge_request_errors)
|
def handle_merge_request_error(merge_request_errors)
|
||||||
if merge_request_errors[:target_branch].any?
|
if merge_request_errors[:target_branch].any?
|
||||||
error!(merge_request_errors[:target_branch], 400)
|
bad_request!(:target_branch)
|
||||||
elsif merge_request_errors[:source_branch].any?
|
elsif merge_request_errors[:source_branch].any?
|
||||||
error!(merge_request_errors[:source_branch], 400)
|
bad_request!(:source_branch)
|
||||||
elsif merge_request_errors[:base].any?
|
elsif merge_request_errors[:base].any?
|
||||||
error!(merge_request_errors[:base], 422)
|
error!(merge_request_errors[:base], 422)
|
||||||
end
|
end
|
||||||
|
@ -129,7 +129,7 @@ module Gitlab
|
||||||
present note, with: Entities::MRNote
|
present note, with: Entities::MRNote
|
||||||
else
|
else
|
||||||
if note.errors[:note].any?
|
if note.errors[:note].any?
|
||||||
error!(note.errors[:note], 400)
|
bad_request!(:note)
|
||||||
end
|
end
|
||||||
not_found!
|
not_found!
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,7 +13,7 @@ module Gitlab
|
||||||
#
|
#
|
||||||
def handle_milestone_errors(milestone_errors)
|
def handle_milestone_errors(milestone_errors)
|
||||||
if milestone_errors[:title].any?
|
if milestone_errors[:title].any?
|
||||||
error!(milestone_errors[:title], 400)
|
bad_request!(:title)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -44,7 +44,7 @@ module Gitlab
|
||||||
present @note, with: Entities::Note
|
present @note, with: Entities::Note
|
||||||
else
|
else
|
||||||
# :note is exposed as :body, but :note is set on error
|
# :note is exposed as :body, but :note is set on error
|
||||||
error!(@note.errors[:note], 400) if @note.errors[:note].any?
|
bad_request!(:note) if @note.errors[:note].any?
|
||||||
not_found!
|
not_found!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -45,7 +45,7 @@ module Gitlab
|
||||||
# Example Request
|
# Example Request
|
||||||
# POST /projects
|
# POST /projects
|
||||||
post do
|
post do
|
||||||
error!("Name is required", 400) if !params.has_key? :name
|
bad_request!(:name) if !params.has_key? :name
|
||||||
attrs = attributes_for_keys [:name,
|
attrs = attributes_for_keys [:name,
|
||||||
:description,
|
:description,
|
||||||
:default_branch,
|
:default_branch,
|
||||||
|
@ -101,8 +101,8 @@ module Gitlab
|
||||||
post ":id/members" do
|
post ":id/members" do
|
||||||
authorize! :admin_project, user_project
|
authorize! :admin_project, user_project
|
||||||
|
|
||||||
error!("User id not given", 400) if !params.has_key? :user_id
|
bad_request!(:user_id) if !params.has_key? :user_id
|
||||||
error!("Access level not given", 400) if !params.has_key? :access_level
|
bad_request!(:access_level) if !params.has_key? :access_level
|
||||||
|
|
||||||
# either the user is already a team member or a new one
|
# either the user is already a team member or a new one
|
||||||
team_member = user_project.team_member_by_id(params[:user_id])
|
team_member = user_project.team_member_by_id(params[:user_id])
|
||||||
|
@ -133,8 +133,8 @@ module Gitlab
|
||||||
authorize! :admin_project, user_project
|
authorize! :admin_project, user_project
|
||||||
|
|
||||||
team_member = user_project.users_projects.find_by_user_id(params[:user_id])
|
team_member = user_project.users_projects.find_by_user_id(params[:user_id])
|
||||||
error!("Access level not given", 400) if !params.has_key? :access_level
|
bad_request!(:access_level) if !params.has_key? :access_level
|
||||||
error!("User can not be found", 404) if team_member.nil?
|
not_found!("User can not be found") if team_member.nil?
|
||||||
|
|
||||||
if team_member.update_attributes(project_access: params[:access_level])
|
if team_member.update_attributes(project_access: params[:access_level])
|
||||||
@member = team_member.user
|
@member = team_member.user
|
||||||
|
@ -196,7 +196,7 @@ module Gitlab
|
||||||
post ":id/hooks" do
|
post ":id/hooks" do
|
||||||
authorize! :admin_project, user_project
|
authorize! :admin_project, user_project
|
||||||
|
|
||||||
error!("Url not given", 400) unless params.has_key? :url
|
bad_request!(:url) unless params.has_key? :url
|
||||||
|
|
||||||
@hook = user_project.hooks.new({"url" => params[:url]})
|
@hook = user_project.hooks.new({"url" => params[:url]})
|
||||||
if @hook.save
|
if @hook.save
|
||||||
|
@ -218,7 +218,7 @@ module Gitlab
|
||||||
@hook = user_project.hooks.find(params[:hook_id])
|
@hook = user_project.hooks.find(params[:hook_id])
|
||||||
authorize! :admin_project, user_project
|
authorize! :admin_project, user_project
|
||||||
|
|
||||||
error!("Url not given", 400) unless params.has_key? :url
|
bad_request!(:url) unless params.has_key? :url
|
||||||
|
|
||||||
attrs = attributes_for_keys [:url]
|
attrs = attributes_for_keys [:url]
|
||||||
if @hook.update_attributes attrs
|
if @hook.update_attributes attrs
|
||||||
|
@ -237,7 +237,7 @@ module Gitlab
|
||||||
# DELETE /projects/:id/hooks
|
# DELETE /projects/:id/hooks
|
||||||
delete ":id/hooks" do
|
delete ":id/hooks" do
|
||||||
authorize! :admin_project, user_project
|
authorize! :admin_project, user_project
|
||||||
error!("Hook id not given", 400) unless params.has_key? :hook_id
|
bad_request!(:hook_id) unless params.has_key? :hook_id
|
||||||
|
|
||||||
begin
|
begin
|
||||||
@hook = ProjectHook.find(params[:hook_id])
|
@hook = ProjectHook.find(params[:hook_id])
|
||||||
|
@ -368,9 +368,9 @@ module Gitlab
|
||||||
post ":id/snippets" do
|
post ":id/snippets" do
|
||||||
authorize! :write_snippet, user_project
|
authorize! :write_snippet, user_project
|
||||||
|
|
||||||
error!("Title not given", 400) if !params[:title].present?
|
bad_request!(:title) if !params[:title].present?
|
||||||
error!("Filename not given", 400) if !params[:file_name].present?
|
bad_request!(:file_name) if !params[:file_name].present?
|
||||||
error!("Code not given", 400) if !params[:code].present?
|
bad_request!(:code) if !params[:code].present?
|
||||||
|
|
||||||
attrs = attributes_for_keys [:title, :file_name]
|
attrs = attributes_for_keys [:title, :file_name]
|
||||||
attrs[:expires_at] = params[:lifetime] if params[:lifetime].present?
|
attrs[:expires_at] = params[:lifetime] if params[:lifetime].present?
|
||||||
|
@ -451,7 +451,7 @@ module Gitlab
|
||||||
get ":id/repository/commits/:sha/blob" do
|
get ":id/repository/commits/:sha/blob" do
|
||||||
authorize! :download_code, user_project
|
authorize! :download_code, user_project
|
||||||
|
|
||||||
error!("Filepath must be specified", 400) if !params.has_key? :filepath
|
bad_request!(:filepath) if !params.has_key? :filepath
|
||||||
|
|
||||||
ref = params[:sha]
|
ref = params[:sha]
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,11 @@ describe Gitlab::API do
|
||||||
response.status.should == 200
|
response.status.should == 200
|
||||||
json_response['title'].should == merge_request.title
|
json_response['title'].should == merge_request.title
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should return a 404 error if merge_request_id not found" do
|
||||||
|
get api("/projects/#{project.id}/merge_request/999", user)
|
||||||
|
response.status.should == 404
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "POST /projects/:id/merge_requests" do
|
describe "POST /projects/:id/merge_requests" do
|
||||||
|
|
Loading…
Reference in a new issue