mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-03 22:06:07 +00:00
Report useful error message in missing parameter
This commit is contained in:
@@ -19,6 +19,7 @@ module Api
|
||||
rescue_from CanCan::AccessDenied, with: :unauthorized
|
||||
rescue_from ActiveRecord::RecordNotFound, with: :not_found
|
||||
rescue_from Pagy::VariableError, with: :invalid_pagination
|
||||
rescue_from ActionController::ParameterMissing, with: :missing_parameter
|
||||
rescue_from ActionController::UnpermittedParameters, with: :unpermitted_parameters
|
||||
|
||||
private
|
||||
@@ -62,6 +63,13 @@ module Api
|
||||
json: json_api_error(exception.message)
|
||||
end
|
||||
|
||||
def missing_parameter(error)
|
||||
message = I18n.t(:missing_parameter, param: error.param, scope: :api)
|
||||
|
||||
render status: :unprocessable_entity,
|
||||
json: json_api_error(message)
|
||||
end
|
||||
|
||||
def unpermitted_parameters(error)
|
||||
message = I18n.t(:unpermitted_parameters, params: error.params.join(", "), scope: :api)
|
||||
|
||||
|
||||
@@ -1413,6 +1413,7 @@ en:
|
||||
invalid_api_key: "Invalid API key (%{key}) specified."
|
||||
unauthorized: "You are not authorized to perform that action."
|
||||
unpermitted_parameters: "Parameters not allowed in this request: %{params}"
|
||||
missing_parameter: "A required parameter is missing or empty: %{param}"
|
||||
invalid_resource: "Invalid resource. Please fix errors and try again."
|
||||
resource_not_found: "The resource you were looking for could not be found."
|
||||
enterprise_logo:
|
||||
|
||||
@@ -161,7 +161,10 @@ describe "Customers", type: :request do
|
||||
param(:customer) { {} }
|
||||
schema "$ref": "#/components/schemas/error_response"
|
||||
|
||||
run_test!
|
||||
run_test! do
|
||||
expect(json_error_detail).to eq "A required parameter is missing or empty: customer"
|
||||
expect(json_response[:meta]).to eq nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user