Report useful error message in missing parameter

This commit is contained in:
Maikel Linke
2022-03-03 14:07:04 +11:00
parent 8d12c7a692
commit aa6e5ae799
3 changed files with 13 additions and 1 deletions

View File

@@ -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)

View File

@@ -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:

View File

@@ -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