From 47c07831c185cb7ec50258809befa1fb4f9b2059 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 8 Aug 2022 15:36:23 +0200 Subject: [PATCH 1/3] Create an ErrorsController that handle errors thrown application --- app/controllers/errors_controller.rb | 17 +++++++++++ .../errors/internal_server_error.html.haml | 8 ++++++ app/views/errors/not_found.html.haml | 8 ++++++ .../errors/unprocessable_entity.html.haml | 26 +++++++++++++++++ app/views/layouts/errors.html.haml | 28 +++++++++++++++++++ config/application.rb | 2 ++ config/locales/en.yml | 22 +++++++++++++++ 7 files changed, 111 insertions(+) create mode 100644 app/controllers/errors_controller.rb create mode 100644 app/views/errors/internal_server_error.html.haml create mode 100644 app/views/errors/not_found.html.haml create mode 100644 app/views/errors/unprocessable_entity.html.haml create mode 100644 app/views/layouts/errors.html.haml diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb new file mode 100644 index 0000000000..7b15a8b8dc --- /dev/null +++ b/app/controllers/errors_controller.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class ErrorsController < ApplicationController + layout "errors" + + def not_found + render status: :not_found + end + + def internal_server_error + render status: :internal_server_error + end + + def unprocessable_entity + render status: :unprocessable_entity + end +end diff --git a/app/views/errors/internal_server_error.html.haml b/app/views/errors/internal_server_error.html.haml new file mode 100644 index 0000000000..3033e1a59c --- /dev/null +++ b/app/views/errors/internal_server_error.html.haml @@ -0,0 +1,8 @@ +- content_for :title do + = I18n.t("errors.internal_server_error.title") +.dialog + %a{href: "/"} + %img{src: "/500.jpg"} + %h1 + = t("errors.internal_server_error.title") + = t("errors.internal_server_error.message_html") diff --git a/app/views/errors/not_found.html.haml b/app/views/errors/not_found.html.haml new file mode 100644 index 0000000000..de473462a7 --- /dev/null +++ b/app/views/errors/not_found.html.haml @@ -0,0 +1,8 @@ +- content_for :title do + = I18n.t("errors.not_found.title") +.dialog + %a{href: "/"} + %img{src: "/500.jpg"} + %h1 + = t("errors.not_found.title") + = t("errors.not_found.message_html") diff --git a/app/views/errors/unprocessable_entity.html.haml b/app/views/errors/unprocessable_entity.html.haml new file mode 100644 index 0000000000..1bc4aea24b --- /dev/null +++ b/app/views/errors/unprocessable_entity.html.haml @@ -0,0 +1,26 @@ +:css + body { + text-align: center; + } + a { + font-size: 100%; + color: black; + line-height: 1.5em; + text-decoration: none; + border-bottom: 1px dotted black; + padding: 0 0.2rem; + } + a:hover, a:focus, a:active{ + background: #8f301d; + color: white; + border-bottom: none; + } + +- content_for :title do + = I18n.t("errors.unprocessable_entity.title") +.dialog + %a{href: "/", style: "border: none; background: none;"} + %img{src: "/422.jpg"} + %h1 + = t("errors.unprocessable_entity.title") + = t("errors.unprocessable_entity.message_html") diff --git a/app/views/layouts/errors.html.haml b/app/views/layouts/errors.html.haml new file mode 100644 index 0000000000..c3d35d472a --- /dev/null +++ b/app/views/layouts/errors.html.haml @@ -0,0 +1,28 @@ +%html +%head + :css + body { + background-color: #fff; + color: #666; + font-family: arial, sans-serif; + + } + div.dialog { + width: 600px; + margin: auto; + } + @media only screen + and (min-width: 320px) + and (max-width: 568px) { + div.dialog, div.dialog img { + width: 95%; + } + } + div.dialog h1 { + font-size: 1.8em; + } + %title + = yield(:title) + +%body + = yield diff --git a/config/application.rb b/config/application.rb index 11e8cf12b8..c7fc66d559 100644 --- a/config/application.rb +++ b/config/application.rb @@ -243,5 +243,7 @@ module Openfoodnetwork config.active_storage.service = ENV["S3_BUCKET"].present? ? :amazon : :local config.active_storage.content_types_to_serve_as_binary -= ["image/svg+xml"] config.active_storage.variable_content_types += ["image/svg+xml"] + + config.exceptions_app = self.routes end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 959e95dc97..9f8712c99b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -4302,3 +4302,25 @@ See the %{link} to find out more about %{sitename}'s features and to start using x_years: one: 1 year other: "%{count} years" + errors: + not_found: + title: "The page you were looking for doesn't exist (404)" + message_html: "Please try again +

This might be a temporary problem. Please click the back button to return to the previous screen or go back to Home and try again.

+ Contact support +

If the problem persists or is urgent, please tell us about it. Find our contact details from the global Open Food Network Local page.

+

It really helps us if you can give as much detail as possible about what the missing page is about.

" + internal_server_error: + title: "We're sorry, but something went wrong (500)" + message_html: "Please try again +

This might be a temporary problem. Please click the back button to return to the previous screen or go back to Home and try again.

+ We're on it +

If you have seen this problem before, we probably already know about it and are working on a fix. We record all the errors that come up.

+ Contact support +

If the problem persists or is urgent, please tell us about it. Find our contact details from the global Open Food Network Local page.

+

It really helps us if you can give as much detail as possible about what you were doing when this error occurred.

" + unprocessable_entity: + title: "The change you wanted was rejected (422)" + message_html: "

The change you wanted was rejected. Maybe you tried to change something you don't have access to. +

Return home

+

" From 60a5bf682b32c42e8e1ab862cce0dc74cfa95b1a Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 8 Aug 2022 15:36:46 +0200 Subject: [PATCH 2/3] Handle errors page through controller ErrorsController --- config/routes.rb | 5 +++++ public/404.html | 41 ----------------------------------------- public/422.html | 38 -------------------------------------- public/500.html | 43 ------------------------------------------- 4 files changed, 5 insertions(+), 122 deletions(-) delete mode 100644 public/404.html delete mode 100644 public/422.html delete mode 100644 public/500.html diff --git a/config/routes.rb b/config/routes.rb index d6a135b3dd..bf8ac6aa01 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -124,4 +124,9 @@ Openfoodnetwork::Application.routes.draw do # Mount Spree's routes mount Spree::Core::Engine, :at => '/' + + # Errors controller + match '/404' => 'errors#not_found', via: :all + match '/500' => 'errors#internal_server_error', via: :all + match '/422' => 'errors#unprocessable_entity', via: :all end diff --git a/public/404.html b/public/404.html deleted file mode 100644 index 4e42e63b73..0000000000 --- a/public/404.html +++ /dev/null @@ -1,41 +0,0 @@ - - - - The page you were looking for doesn't exist (404) - - - - - -
- -

Oops! Page not found.

- Please try again -

This might be a temporary problem. Please click the back button to return to the previous screen or go back to Home and try again.

- Contact support -

If the problem persists or is urgent, please tell us about it. Find our contact details from the global Open Food Network Local page.

-

It really helps us if you can give as much detail as possible about what the missing page is about.

-
- - diff --git a/public/422.html b/public/422.html deleted file mode 100644 index c6a93d7ab6..0000000000 --- a/public/422.html +++ /dev/null @@ -1,38 +0,0 @@ - - - - The change you wanted was rejected (422) - - - - - -
- -

The change you wanted was rejected. Maybe you tried to change something you don't have access to. -

Return home

-

-
- - - diff --git a/public/500.html b/public/500.html deleted file mode 100644 index b3dd25194a..0000000000 --- a/public/500.html +++ /dev/null @@ -1,43 +0,0 @@ - - - - We're sorry, but something went wrong (500) - - - - - -
- -

We're sorry, but something went wrong.

- Please try again -

This might be a temporary problem. Please click the back button to return to the previous screen or go back to Home and try again.

- We're on it -

If you have seen this problem before, we probably already know about it and are working on a fix. We record all the errors that come up.

- Contact support -

If the problem persists or is urgent, please tell us about it. Find our contact details from the global Open Food Network Local page.

-

It really helps us if you can give as much detail as possible about what you were doing when this error occurred.

-
- - From f474947d2dc686525cf9bd6ca3464536db1076e0 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 11 Aug 2022 11:33:33 +0200 Subject: [PATCH 3/3] Do not remove `500.html` file since it is used by nxginx in case of critical problems when application cannot respond --- public/500.html | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 public/500.html diff --git a/public/500.html b/public/500.html new file mode 100644 index 0000000000..b3dd25194a --- /dev/null +++ b/public/500.html @@ -0,0 +1,43 @@ + + + + We're sorry, but something went wrong (500) + + + + + +
+ +

We're sorry, but something went wrong.

+ Please try again +

This might be a temporary problem. Please click the back button to return to the previous screen or go back to Home and try again.

+ We're on it +

If you have seen this problem before, we probably already know about it and are working on a fix. We record all the errors that come up.

+ Contact support +

If the problem persists or is urgent, please tell us about it. Find our contact details from the global Open Food Network Local page.

+

It really helps us if you can give as much detail as possible about what you were doing when this error occurred.

+
+ +