Commit d033d6c7 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'mk/fix-bad-request-helper' into 'master'

Make `bad_request!` API helper more generic

See merge request gitlab-org/gitlab!49895
parents d606041f 053196d0
---
title: Fix wording of some 400 Bad request API responses
merge_request: 49895
author:
type: fixed
......@@ -70,7 +70,7 @@ RSpec.describe API::Analytics::ProjectDeploymentFrequency do
it 'returns `bad_request` with expected message' do
expect(response.parsed_body).to eq({
"message" => "400 (Bad request) \"Date range is greater than 91 days\" not given"
"message" => "400 Bad request - Date range is greater than 91 days"
})
end
end
......@@ -82,7 +82,7 @@ RSpec.describe API::Analytics::ProjectDeploymentFrequency do
it 'returns `bad_request` with expected message' do
expect(response.parsed_body).to eq({
"message" => "400 (Bad request) \"Parameter `to` is before the `from` date\" not given"
"message" => "400 Bad request - Parameter `to` is before the `from` date"
})
end
end
......
......@@ -37,7 +37,7 @@ module API
get 'dist-tags', format: false, requirements: ::API::Helpers::Packages::Npm::NPM_ENDPOINT_REQUIREMENTS do
package_name = params[:package_name]
bad_request!('Package Name') if package_name.blank?
bad_request_missing_attribute!('Package Name') if package_name.blank?
authorize_read_package!(project)
......@@ -62,9 +62,9 @@ module API
version = env['api.request.body']
tag = params[:tag]
bad_request!('Package Name') if package_name.blank?
bad_request!('Version') if version.blank?
bad_request!('Tag') if tag.blank?
bad_request_missing_attribute!('Package Name') if package_name.blank?
bad_request_missing_attribute!('Version') if version.blank?
bad_request_missing_attribute!('Tag') if tag.blank?
authorize_create_package!(project)
......@@ -85,8 +85,8 @@ module API
package_name = params[:package_name]
tag = params[:tag]
bad_request!('Package Name') if package_name.blank?
bad_request!('Tag') if tag.blank?
bad_request_missing_attribute!('Package Name') if package_name.blank?
bad_request_missing_attribute!('Tag') if tag.blank?
authorize_destroy_package!(project)
......
......@@ -48,7 +48,7 @@ module API
not_found! unless Feature.enabled?(:go_proxy, user_project)
module_name = case_decode params[:module_name]
bad_request!('Module Name') if module_name.blank?
bad_request_missing_attribute!('Module Name') if module_name.blank?
mod = ::Packages::Go::ModuleFinder.new(user_project, module_name).execute
......
......@@ -322,7 +322,7 @@ module API
# keys (required) - A hash consisting of keys that must be present
def required_attributes!(keys)
keys.each do |key|
bad_request!(key) unless params[key].present?
bad_request_missing_attribute!(key) unless params[key].present?
end
end
......@@ -368,12 +368,16 @@ module API
render_api_error!(message.join(' '), 403)
end
def bad_request!(attribute)
message = ["400 (Bad request)"]
message << "\"" + attribute.to_s + "\" not given" if attribute
def bad_request!(reason = nil)
message = ['400 Bad request']
message << "- #{reason}" if reason
render_api_error!(message.join(' '), 400)
end
def bad_request_missing_attribute!(attribute)
bad_request!("\"#{attribute}\" not given")
end
def not_found!(resource = nil)
message = ["404"]
message << resource if resource
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment