Merge pull request #3332 from betagouv/present-carto-api-errors

Afficher les erreurs d'API carto à l'usager
This commit is contained in:
Pierre de La Morinerie 2019-01-28 18:27:48 +01:00 committed by GitHub
commit 2215d3e829
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 68 additions and 34 deletions

View file

@ -7,3 +7,6 @@ $default-padding: 2 * $default-spacer;
// layouts
$two-columns-padding: 60px;
$two-columns-breakpoint: $page-width + (2 * $two-columns-padding);
// z-order
$alert-z-index: 100;

View file

@ -1,4 +1,5 @@
@import "colors";
@import "constants";
.alert {
padding: 15px;
@ -24,8 +25,10 @@
.alert-fixed {
position: fixed;
left: 50%;
margin-left: -100px;
width: 200px;
transform: translate(-50%);
max-width: 700px;
top: 10px;
border-radius: 10px;
// Ensure fixed flash messages are above `position: absolute` elements (like maps)
z-index: $alert-z-index;
}

View file

@ -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

View file

@ -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, RestClient::BadGateway, RestClient::GatewayTimeout => e
Rails.logger.error "[ApiCarto] Error on #{url}: #{e}"
raise RestClient::ResourceNotFound
end
end

View file

@ -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 }) %>

View file

@ -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