diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index 2cc3afd1c..54930edf9 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -1,6 +1,8 @@ module Api module V1 class BaseController < ActionController::API + rescue_from ActiveRecord::RecordNotFound, with: :not_found_error + private def authenticate @@ -8,6 +10,12 @@ module Api head :unauthorized unless ip_allowed end + def not_found_error + uuid = params['uuid'] + json = { error: 'Not Found', uuid: uuid, message: 'Record not found' } + render json: json, status: :not_found + end + def allowed_ips ENV['auction_api_allowed_ips'].split(',').map(&:strip) end diff --git a/test/integration/api/v1/auctions/details_test.rb b/test/integration/api/v1/auctions/details_test.rb index f1cab52af..374051258 100644 --- a/test/integration/api/v1/auctions/details_test.rb +++ b/test/integration/api/v1/auctions/details_test.rb @@ -25,8 +25,11 @@ class ApiV1AuctionDetailsTest < ActionDispatch::IntegrationTest end def test_auction_not_found - assert_raises ActiveRecord::RecordNotFound do - get api_v1_auction_path('non-existing-uuid'), as: :json - end + expected_uuid = 'not-a-real-path' + get api_v1_auction_path(expected_uuid), as: :json + assert_response :not_found + json = JSON.parse(response.body, symbolize_names: true) + assert_equal expected_uuid, json[:uuid] + assert_equal 'Not Found', json[:error] end -end \ No newline at end of file +end diff --git a/test/integration/api/v1/auctions/update_test.rb b/test/integration/api/v1/auctions/update_test.rb index e6ce3c683..5b00a1052 100644 --- a/test/integration/api/v1/auctions/update_test.rb +++ b/test/integration/api/v1/auctions/update_test.rb @@ -141,10 +141,14 @@ class ApiV1AuctionUpdateTest < ActionDispatch::IntegrationTest end def test_auction_not_found - assert_raises ActiveRecord::RecordNotFound do - patch api_v1_auction_path('non-existing-uuid'), - params: { status: Auction.statuses[:no_bids] }, - as: :json - end + expected_uuid = 'non-existing-uuid' + patch api_v1_auction_path(expected_uuid), + params: { status: Auction.statuses[:no_bids] }, + as: :json + + assert_response :not_found + json = JSON.parse(response.body, symbolize_names: true) + assert_equal expected_uuid, json[:uuid] + assert_equal 'Not Found', json[:error] end end