Skip to content

Commit

Permalink
fix error responses so json returned in production
Browse files Browse the repository at this point in the history
  • Loading branch information
timcowlishaw committed Oct 26, 2023
1 parent 4a33aa7 commit 800e6d5
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 44 deletions.
2 changes: 1 addition & 1 deletion app/controllers/v0/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def raise_ransack_errors_as_bad_request(&block)
begin
block.call
rescue ArgumentError => e
raise ActionController::BadRequest.new(e.message)
render json: { message: e.message, status: 400 }, status: 400
end
end

Expand Down
43 changes: 21 additions & 22 deletions app/controllers/v0/devices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,30 @@ def index
@q = policy_scope(Device)
.includes(:owner, :tags, kit: [:components, :sensors])
.ransack(params[:q], auth_object: (current_user&.is_admin? ? :admin : nil))
end
# We are here customly adding multiple tags into the Ransack query.
# Ransack supports this, but how do we add multiple tag names in URL string? Which separator to use?
# See Issue #186 https://github.com/fablabbcn/smartcitizen-api/issues/186
# If we figure it out, we can remove the next 3 lines, but remember to document in:
# https://developer.smartcitizen.me/#basic-searching
if params[:with_tags]
@q.tags_name_in = params[:with_tags].split('|')
end
# We are here customly adding multiple tags into the Ransack query.
# Ransack supports this, but how do we add multiple tag names in URL string? Which separator to use?
# See Issue #186 https://github.com/fablabbcn/smartcitizen-api/issues/186
# If we figure it out, we can remove the next 3 lines, but remember to document in:
# https://developer.smartcitizen.me/#basic-searching
if params[:with_tags]
@q.tags_name_in = params[:with_tags].split('|')
end

@devices = @q.result(distinct: true)

if params[:near]
if params[:near] =~ /\A(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)\z/
@devices = @devices.near(
params[:near].split(','), (params[:within] || 1000))
else
return render json: { id: "bad_request",
message: "Malformed near parameter",
url: 'https://developer.smartcitizen.me/#get-all-devices',
errors: nil }, status: :bad_request
@devices = @q.result(distinct: true)

if params[:near]
if params[:near] =~ /\A(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)\z/
@devices = @devices.near(
params[:near].split(','), (params[:within] || 1000))
else
return render json: { id: "bad_request",
message: "Malformed near parameter",
url: 'https://developer.smartcitizen.me/#get-all-devices',
errors: nil }, status: :bad_request
end
end
@devices = paginate(@devices)
end

@devices = paginate(@devices)
end

def update
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/v0/sensors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def show
def index
raise_ransack_errors_as_bad_request do
@q = Sensor.includes(:measurement, :tag_sensors).ransack(params[:q])
@q.sorts = 'id asc' if @q.sorts.empty?
@sensors = @q.result(distinct: true)
@sensors = paginate @sensors
end
@q.sorts = 'id asc' if @q.sorts.empty?
@sensors = @q.result(distinct: true)
@sensors = paginate @sensors
end

def create
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/v0/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ def show
def index
raise_ransack_errors_as_bad_request do
@q = User.includes(:devices, :profile_picture_attachment).ransack(params[:q])
@q.sorts = 'id asc' if @q.sorts.empty?
@users = @q.result(distinct: true)
@users = paginate(@users)
end
@q.sorts = 'id asc' if @q.sorts.empty?
@users = @q.result(distinct: true)
@users = paginate(@users)
end

def create
Expand Down
12 changes: 6 additions & 6 deletions spec/requests/v0/devices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@
end

it "does not allow searching by mac address by non-admins" do
expect {
api_get "devices?q[mac_address_eq]=00:00:00:00:00:00"
}.to raise_error(ActionController::BadRequest)
json = api_get "devices?q[mac_address_eq]=00:00:00:00:00:00"
expect(response.status).to eq(400)
expect(json["status"]).to eq(400)
end

it "does not allow searching on disallowed parameters" do
expect {
api_get "devices?q[disallowed_eq]=1"
}.to raise_error(ActionController::BadRequest)
json = api_get "devices?q[disallowed_eq]=1"
expect(response.status).to eq(400)
expect(json["status"]).to eq(400)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/requests/v0/sensors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@
end

it "does not allow searching on disallowed parameters" do
expect {
api_get "sensors?q[disallowed_eq]=1"
}.to raise_error(ActionController::BadRequest)
json = api_get "sensors?q[disallowed_eq]=1"
expect(response.status).to eq(400)
expect(json["status"]).to eq(400)
end

end
Expand Down
12 changes: 6 additions & 6 deletions spec/requests/v0/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@

describe "smoke tests for ransack" do
it "does not allow searching by first name" do
expect {
api_get "users?q[first_name_eq]=Tim"
}.to raise_error(ActionController::BadRequest)
json = api_get "users?q[first_name_eq]=Tim"
expect(response.status).to eq(400)
expect(json["status"]).to eq(400)
end

it "allows searching by city" do
Expand Down Expand Up @@ -85,9 +85,9 @@
end

it "does not allow searching on disallowed parameters" do
expect {
api_get "users?q[disallowed_eq]=1"
}.to raise_error(ActionController::BadRequest)
json = api_get "users?q[disallowed_eq]=1"
expect(response.status).to eq(400)
expect(json["status"]).to eq(400)
end
end
end
Expand Down

0 comments on commit 800e6d5

Please sign in to comment.