From 63a55a8a0b758a2da88e7d697cd7d33f170f4274 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sun, 2 Dec 2018 00:43:11 +0100 Subject: [PATCH] nginx: error pages are now respond as json if accept header request it. As a consequence, we can rely on them to display a correct information on user pages through angularJS. --- configs/nginx-fic-static.conf | 25 ++++++++++++++++++++++++ configs/nginx-frontend-htpasswd.conf | 25 ++++++++++++++++++++++++ configs/nginx-prod.conf | 12 ++++++++++++ frontend/static/e404.html | 7 ++++++- frontend/static/e404.json | 1 + frontend/static/e413.html | 7 ++++++- frontend/static/e413.json | 1 + frontend/static/e500.html | 9 +++++++-- frontend/static/e500.json | 1 + frontend/static/js/challenge.js | 29 ++++++++++++---------------- frontend/static/welcome.html | 12 ++++++------ frontend/static/welcome.json | 1 + 12 files changed, 103 insertions(+), 27 deletions(-) create mode 100644 frontend/static/e404.json create mode 100644 frontend/static/e413.json create mode 100644 frontend/static/e500.json create mode 100644 frontend/static/welcome.json diff --git a/configs/nginx-fic-static.conf b/configs/nginx-fic-static.conf index 6f5ad1f5..2d0c7427 100644 --- a/configs/nginx-fic-static.conf +++ b/configs/nginx-fic-static.conf @@ -35,4 +35,29 @@ server { location /tags/ { rewrite ^/.*$ /index.html; } + + location = /welcome.html { + internal; + if ($http_accept ~ "^application/json") { + rewrite ^/(.*).html$ /$1.json; + } + } + location = /e404.html { + internal; + if ($http_accept ~ "^application/json") { + rewrite ^/(.*).html$ /$1.json; + } + } + location = /e413.html { + internal; + if ($http_accept ~ "^application/json") { + rewrite ^/(.*).html$ /$1.json; + } + } + location = /e500.html { + internal; + if ($http_accept ~ "^application/json") { + rewrite ^/(.*).html$ /$1.json; + } + } } diff --git a/configs/nginx-frontend-htpasswd.conf b/configs/nginx-frontend-htpasswd.conf index f47565ca..b565353d 100644 --- a/configs/nginx-frontend-htpasswd.conf +++ b/configs/nginx-frontend-htpasswd.conf @@ -22,6 +22,31 @@ server { #auth_basic_user_file ficpasswd; } + location = /welcome.html { + internal; + if ($http_accept ~ "^application/json") { + rewrite ^/(.*).html$ /$1.json; + } + } + location = /e404.html { + internal; + if ($http_accept ~ "^application/json") { + rewrite ^/(.*).html$ /$1.json; + } + } + location = /e413.html { + internal; + if ($http_accept ~ "^application/json") { + rewrite ^/(.*).html$ /$1.json; + } + } + location = /e500.html { + internal; + if ($http_accept ~ "^application/json") { + rewrite ^/(.*).html$ /$1.json; + } + } + location /edit { rewrite ^/.*$ /index.html; } diff --git a/configs/nginx-prod.conf b/configs/nginx-prod.conf index 391488a7..7938b200 100644 --- a/configs/nginx-prod.conf +++ b/configs/nginx-prod.conf @@ -41,15 +41,27 @@ server { } location = /welcome.html { internal; + if ($http_accept ~ "^application/json") { + rewrite ^/(.*).html$ /$1.json; + } } location = /e404.html { internal; + if ($http_accept ~ "^application/json") { + rewrite ^/(.*).html$ /$1.json; + } } location = /e413.html { internal; + if ($http_accept ~ "^application/json") { + rewrite ^/(.*).html$ /$1.json; + } } location = /e500.html { internal; + if ($http_accept ~ "^application/json") { + rewrite ^/(.*).html$ /$1.json; + } } location ~ ^/[A-Z] { diff --git a/frontend/static/e404.html b/frontend/static/e404.html index b4e53043..15c9dc51 100644 --- a/frontend/static/e404.html +++ b/frontend/static/e404.html @@ -10,6 +10,11 @@ + @@ -30,7 +35,7 @@
-
+

Page introuvable Erreur 404


diff --git a/frontend/static/e404.json b/frontend/static/e404.json new file mode 100644 index 00000000..e92fea36 --- /dev/null +++ b/frontend/static/e404.json @@ -0,0 +1 @@ +{"errmsg": "La page à laquelle vous tentez d'accéder n'existe pas ou l'adresse que vous avez tapée est incorrecte."} diff --git a/frontend/static/e413.html b/frontend/static/e413.html index 54b75be7..42bec373 100644 --- a/frontend/static/e413.html +++ b/frontend/static/e413.html @@ -10,6 +10,11 @@ + @@ -30,7 +35,7 @@

-
+

Requête trop grosse Erreur 413


diff --git a/frontend/static/e413.json b/frontend/static/e413.json new file mode 100644 index 00000000..194d473c --- /dev/null +++ b/frontend/static/e413.json @@ -0,0 +1 @@ +{"errmsg": "La quantité de données que vous souhaitez envoyer au serveur est trop importante pour qu'il accepte de la traiter."} diff --git a/frontend/static/e500.html b/frontend/static/e500.html index 8e96e27f..2e2588d8 100644 --- a/frontend/static/e500.html +++ b/frontend/static/e500.html @@ -10,6 +10,11 @@ + @@ -30,11 +35,11 @@

-
+

Erreur interne Erreur 500


- Notre serveur est actuellement dans l'incapacité de repondre à votre requête.
Veuillez recommencer dans quelques instants. + Notre serveur est actuellement dans l'incapacité de répondre à votre requête.
Veuillez recommencer dans quelques instants.

Si le problème persiste, contactez un administrateur. diff --git a/frontend/static/e500.json b/frontend/static/e500.json new file mode 100644 index 00000000..43ee1302 --- /dev/null +++ b/frontend/static/e500.json @@ -0,0 +1 @@ +{"errmsg": "Notre serveur est actuellement dans l'incapacité de répondre à votre requête. \nVeuillez recommencer dans quelques instants."} diff --git a/frontend/static/js/challenge.js b/frontend/static/js/challenge.js index 02e5c0e6..788945c1 100644 --- a/frontend/static/js/challenge.js +++ b/frontend/static/js/challenge.js @@ -293,17 +293,13 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"]) }; }) .controller("SubmissionController", function($scope, $http, $rootScope, $timeout) { - $rootScope.sberr = ""; + $scope.sberr = null; var cbs; var cbd; $scope.$watch("my", function(my) { - if (!my || !my.exercices || !my.exercices[$rootScope.current_exercice]) { - if (cbs) - $timeout.cancel(cbs); - cbs = $timeout(waitMy, 420); - } else { + if (my && my.exercices && my.exercices[$rootScope.current_exercice]) { angular.forEach(my.exercices[$rootScope.current_exercice].mcqs, function(mcq,qid) { angular.forEach(mcq["choices"], function(choice,cid) { this[cid] = { @@ -322,6 +318,7 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"]) $scope.ssubmit = function() { var resp = {} var check = undefined + $scope.sberr = null; if ($scope.my.exercices[$rootScope.current_exercice].flags && Object.keys($scope.my.exercices[$rootScope.current_exercice].flags).length) { @@ -384,10 +381,10 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"]) return; } - $http({ url: "/submit/" + $rootScope.current_exercice, method: "POST", data: resp }).then(function(response, status, header, config) { - $rootScope.messageClass = {"text-success": true}; - $rootScope.message = response.data.errmsg; - $rootScope.sberr = ""; + $http({ url: "/submit/" + $rootScope.current_exercice, method: "POST", data: resp }).then(function(response) { + $scope.messageClass = {"text-success": true}; + $scope.message = response.data.errmsg; + $scope.sberr = null; angular.forEach($scope.flags, function(flag,kid) { flag.value = ""; @@ -407,15 +404,13 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"]) }); }; checkDiff(); - }, function(response, status, header, config) { - if (status >= 500) { + }, function(response) { + if (response.status >= 500) { $scope.my.exercices[$rootScope.current_exercice].submitted = false; } - $rootScope.messageClass = {"text-danger": true}; - $rootScope.message = response.data.errmsg; - if (status != 402) { - $rootScope.sberr = "Une erreur est survenue lors de l'envoi. Veuillez réessayer dans quelques instants."; - } + $scope.messageClass = {"text-danger": true}; + $scope.sberr = "Oups !"; + $scope.message = response.data.errmsg; }); $scope.my.exercices[$rootScope.current_exercice].submitted = true; }; diff --git a/frontend/static/welcome.html b/frontend/static/welcome.html index 2159d84e..fb3d5ec3 100644 --- a/frontend/static/welcome.html +++ b/frontend/static/welcome.html @@ -30,7 +30,7 @@

-
+

Bienvenue !

@@ -50,7 +50,7 @@

-
+

Installation du certificat client

@@ -92,7 +92,7 @@
-
+

Mozilla Firefox

@@ -109,7 +109,7 @@
-
+

Chromium/Google Chrome

@@ -141,7 +141,7 @@
-
+

Internet Explorer

@@ -168,7 +168,7 @@
-
+

Safari

diff --git a/frontend/static/welcome.json b/frontend/static/welcome.json new file mode 100644 index 00000000..15f5719b --- /dev/null +++ b/frontend/static/welcome.json @@ -0,0 +1 @@ +{"errmsg": "Vous n'avez pas les droits nécessaires pour effectuer cette action."}