Render pagination errors

This commit is contained in:
Maikel Linke
2022-02-15 15:49:47 +11:00
parent b89715149c
commit 4aa70c1ffd
2 changed files with 17 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ module Api
rescue_from Exception, with: :error_during_processing
rescue_from CanCan::AccessDenied, with: :unauthorized
rescue_from ActiveRecord::RecordNotFound, with: :not_found
rescue_from Pagy::VariableError, with: :invalid_pagination
private
@@ -49,6 +50,11 @@ module Api
json: json_api_error(exception.message, backtrace: exception.backtrace)
end
def invalid_pagination(exception)
render status: :unprocessable_entity,
json: json_api_error(exception.message)
end
def invalid_resource!(resource = nil)
render status: :unprocessable_entity,
json: json_api_invalid(

View File

@@ -81,6 +81,17 @@ describe "Customers", type: :request do
get "/api/v1/customers", params: { page: "2", per_page: "1" }
expect(json_response_ids).to eq [customer2.id.to_s]
end
it "renders beyond the available pages" do
get "/api/v1/customers", params: { page: "2" }
expect(json_response_ids).to eq []
end
it "informs about invalid pages" do
get "/api/v1/customers", params: { page: "0" }
expect(json_response_ids).to eq nil
expect(json_error_detail).to eq 'expected :page >= 1; got "0"'
end
end
post "Create customer" do