From 803364490d49f2dfae85bbc28c6b8311c6ade5cd Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Thu, 24 Jan 2019 18:30:14 +0100 Subject: [PATCH] carto: present API InternalServerError to the user, instead of failing --- app/controllers/champs/carte_controller.rb | 4 + app/lib/api_carto/api.rb | 4 +- app/views/champs/carte/show.js.erb | 2 + .../champs/carte_controller_spec.rb | 82 ++++++++++++------- 4 files changed, 60 insertions(+), 32 deletions(-) diff --git a/app/controllers/champs/carte_controller.rb b/app/controllers/champs/carte_controller.rb index eb799ea92..5374c5111 100644 --- a/app/controllers/champs/carte_controller.rb +++ b/app/controllers/champs/carte_controller.rb @@ -74,5 +74,9 @@ class Champs::CarteController < ApplicationController if @champ.persisted? @champ.save end + + rescue RestClient::ResourceNotFound + flash.alert = 'Les données cartographiques sont temporairement indisponibles. Réessayez dans un instant.' + response.status = 503 end end diff --git a/app/lib/api_carto/api.rb b/app/lib/api_carto/api.rb index 80cadb1b0..651cc97eb 100644 --- a/app/lib/api_carto/api.rb +++ b/app/lib/api_carto/api.rb @@ -13,10 +13,10 @@ class ApiCarto::API def self.call(url, geojson) params = geojson.to_s - RestClient.post(url, params, content_type: 'application/json') - rescue RestClient::InternalServerError + rescue RestClient::InternalServerError => e + Rails.logger.error "[ApiCarto] Error on #{url}: #{e}" raise RestClient::ResourceNotFound end end diff --git a/app/views/champs/carte/show.js.erb b/app/views/champs/carte/show.js.erb index 45ae01b28..e5c1e7055 100644 --- a/app/views/champs/carte/show.js.erb +++ b/app/views/champs/carte/show.js.erb @@ -1,3 +1,5 @@ +<%= render_flash(timeout: 5000, fixed: true) %> + <%= render_to_element("#{@selector} + .geo-areas", partial: 'shared/champs/carte/geo_areas', locals: { champ: @champ, error: @error }) %> diff --git a/spec/controllers/champs/carte_controller_spec.rb b/spec/controllers/champs/carte_controller_spec.rb index a530593eb..374574e09 100644 --- a/spec/controllers/champs/carte_controller_spec.rb +++ b/spec/controllers/champs/carte_controller_spec.rb @@ -25,43 +25,65 @@ describe Champs::CarteController, type: :controller do describe 'POST #show' do render_views - before { sign_in user } - before do - allow_any_instance_of(ApiCarto::QuartiersPrioritairesAdapter) - .to receive(:results) - .and_return([{ code: "QPCODE1234", geometry: { type: "MultiPolygon", coordinates: [[[[2.38715792094576, 48.8723062632126], [2.38724851642619, 48.8721392348061]]]] } }]) + context 'when the API is available' do + render_views - post :show, params: params, format: 'js' + before do + sign_in user + + allow_any_instance_of(ApiCarto::QuartiersPrioritairesAdapter) + .to receive(:results) + .and_return([{ code: "QPCODE1234", geometry: { type: "MultiPolygon", coordinates: [[[[2.38715792094576, 48.8723062632126], [2.38724851642619, 48.8721392348061]]]] } }]) + + post :show, params: params, format: 'js' + end + + context 'when coordinates are empty' do + let(:value) { '[]' } + + it { + expect(assigns(:error)).to eq(nil) + expect(champ.reload.value).to eq(nil) + expect(champ.reload.geo_areas).to eq([]) + expect(response.body).to include("DS.drawMapData(\".carte-1\", {\"position\":{\"lon\":\"2.428462\",\"lat\":\"46.538192\",\"zoom\":\"13\"},\"selection\":null,\"quartiersPrioritaires\":[],\"cadastres\":[],\"parcellesAgricoles\":[]});") + } + end + + context 'when coordinates are informed' do + let(:value) { [[{ "lat": 48.87442541960633, "lng": 2.3859214782714844 }, { "lat": 48.87273183590832, "lng": 2.3850631713867183 }, { "lat": 48.87081237174292, "lng": 2.3809432983398438 }, { "lat": 48.8712640169951, "lng": 2.377510070800781 }, { "lat": 48.87510283703279, "lng": 2.3778533935546875 }, { "lat": 48.87544154230615, "lng": 2.382831573486328 }, { "lat": 48.87442541960633, "lng": 2.3859214782714844 }]].to_json } + + it { expect(response.body).not_to be_nil } + it { expect(response.body).to include('MultiPolygon') } + it { expect(response.body).to include('[2.38715792094576,48.8723062632126]') } + end + + context 'when error' do + let(:geojson) { [[{ "lat": 48.87442541960633, "lng": 2.3859214782714844 }, { "lat": 48.87273183590832, "lng": 2.3850631713867183 }, { "lat": 48.87081237174292, "lng": 2.3809432983398438 }, { "lat": 48.8712640169951, "lng": 2.377510070800781 }, { "lat": 48.87510283703279, "lng": 2.3778533935546875 }, { "lat": 48.87544154230615, "lng": 2.382831573486328 }, { "lat": 48.87442541960633, "lng": 2.3859214782714844 }]] } + let(:value) { '' } + + it { + expect(assigns(:error)).to eq(true) + expect(champ.reload.value).to eq(nil) + expect(champ.reload.geo_areas).to eq([]) + } + end end - context 'when coordinates are empty' do - let(:value) { '[]' } + context 'when the API is unavailable' do + before do + sign_in user - it { - expect(assigns(:error)).to eq(nil) - expect(champ.reload.value).to eq(nil) - expect(champ.reload.geo_areas).to eq([]) - expect(response.body).to include("DS.drawMapData(\".carte-1\", {\"position\":{\"lon\":\"2.428462\",\"lat\":\"46.538192\",\"zoom\":\"13\"},\"selection\":null,\"quartiersPrioritaires\":[],\"cadastres\":[],\"parcellesAgricoles\":[]});") - } - end + allow_any_instance_of(ApiCarto::QuartiersPrioritairesAdapter) + .to receive(:results) + .and_raise(RestClient::ResourceNotFound) + + post :show, params: params, format: 'js' + end - context 'when coordinates are informed' do let(:value) { [[{ "lat": 48.87442541960633, "lng": 2.3859214782714844 }, { "lat": 48.87273183590832, "lng": 2.3850631713867183 }, { "lat": 48.87081237174292, "lng": 2.3809432983398438 }, { "lat": 48.8712640169951, "lng": 2.377510070800781 }, { "lat": 48.87510283703279, "lng": 2.3778533935546875 }, { "lat": 48.87544154230615, "lng": 2.382831573486328 }, { "lat": 48.87442541960633, "lng": 2.3859214782714844 }]].to_json } - it { expect(response.body).not_to be_nil } - it { expect(response.body).to include('MultiPolygon') } - it { expect(response.body).to include('[2.38715792094576,48.8723062632126]') } - end - - context 'when error' do - let(:geojson) { [[{ "lat": 48.87442541960633, "lng": 2.3859214782714844 }, { "lat": 48.87273183590832, "lng": 2.3850631713867183 }, { "lat": 48.87081237174292, "lng": 2.3809432983398438 }, { "lat": 48.8712640169951, "lng": 2.377510070800781 }, { "lat": 48.87510283703279, "lng": 2.3778533935546875 }, { "lat": 48.87544154230615, "lng": 2.382831573486328 }, { "lat": 48.87442541960633, "lng": 2.3859214782714844 }]] } - let(:value) { '' } - - it { - expect(assigns(:error)).to eq(true) - expect(champ.reload.value).to eq(nil) - expect(champ.reload.geo_areas).to eq([]) - } + it { expect(response.status).to eq 503 } + it { expect(response.body).to include('Les données cartographiques sont temporairement indisponibles') } end end end