mirror of
https://github.com/internetee/registry.git
synced 2025-06-06 20:55:44 +02:00
Return JSON response from 404
This commit is contained in:
parent
1033ddc218
commit
7ece2e0341
3 changed files with 24 additions and 9 deletions
|
@ -1,6 +1,8 @@
|
||||||
module Api
|
module Api
|
||||||
module V1
|
module V1
|
||||||
class BaseController < ActionController::API
|
class BaseController < ActionController::API
|
||||||
|
rescue_from ActiveRecord::RecordNotFound, with: :not_found_error
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def authenticate
|
def authenticate
|
||||||
|
@ -8,6 +10,12 @@ module Api
|
||||||
head :unauthorized unless ip_allowed
|
head :unauthorized unless ip_allowed
|
||||||
end
|
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
|
def allowed_ips
|
||||||
ENV['auction_api_allowed_ips'].split(',').map(&:strip)
|
ENV['auction_api_allowed_ips'].split(',').map(&:strip)
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,8 +25,11 @@ class ApiV1AuctionDetailsTest < ActionDispatch::IntegrationTest
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_auction_not_found
|
def test_auction_not_found
|
||||||
assert_raises ActiveRecord::RecordNotFound do
|
expected_uuid = 'not-a-real-path'
|
||||||
get api_v1_auction_path('non-existing-uuid'), as: :json
|
get api_v1_auction_path(expected_uuid), as: :json
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -141,10 +141,14 @@ class ApiV1AuctionUpdateTest < ActionDispatch::IntegrationTest
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_auction_not_found
|
def test_auction_not_found
|
||||||
assert_raises ActiveRecord::RecordNotFound do
|
expected_uuid = 'non-existing-uuid'
|
||||||
patch api_v1_auction_path('non-existing-uuid'),
|
patch api_v1_auction_path(expected_uuid),
|
||||||
params: { status: Auction.statuses[:no_bids] },
|
params: { status: Auction.statuses[:no_bids] },
|
||||||
as: :json
|
as: :json
|
||||||
end
|
|
||||||
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue