From 5fa0aebe9fee869a4a988859dd454a28ee451b97 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 1 Aug 2018 18:56:11 +0100 Subject: [PATCH] Use dynamic error pages built through the asset pipeline Fixes #1241 --- .rubocop_todo.yml | 2 +- app/assets/stylesheets/errors.scss | 8 +++++ app/controllers/errors_controller.rb | 15 +++++++++ app/views/errors/forbidden.html.erb | 3 ++ .../errors/internal_server_error.html.erb | 4 +++ app/views/errors/not_found.html.erb | 3 ++ app/views/layouts/error.html.erb | 14 ++++++++ config/initializers/assets.rb | 1 + config/initializers/errors.rb | 1 + config/routes.rb | 5 +++ public/403.html | 12 ------- public/404.html | 12 ------- public/500.html | 13 -------- test/controllers/errors_controller_test.rb | 33 +++++++++++++++++++ 14 files changed, 88 insertions(+), 38 deletions(-) create mode 100644 app/assets/stylesheets/errors.scss create mode 100644 app/controllers/errors_controller.rb create mode 100644 app/views/errors/forbidden.html.erb create mode 100644 app/views/errors/internal_server_error.html.erb create mode 100644 app/views/errors/not_found.html.erb create mode 100644 app/views/layouts/error.html.erb create mode 100644 config/initializers/errors.rb delete mode 100644 public/403.html delete mode 100644 public/404.html delete mode 100644 public/500.html create mode 100644 test/controllers/errors_controller_test.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a64be7273..f012237b3 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -54,7 +54,7 @@ Metrics/AbcSize: # Offense count: 41 # Configuration parameters: CountComments, ExcludedMethods. Metrics/BlockLength: - Max: 258 + Max: 261 # Offense count: 11 # Configuration parameters: CountBlocks. diff --git a/app/assets/stylesheets/errors.scss b/app/assets/stylesheets/errors.scss new file mode 100644 index 000000000..fd1400232 --- /dev/null +++ b/app/assets/stylesheets/errors.scss @@ -0,0 +1,8 @@ +.logo { + float: left; + margin: 10px; +} + +.details { + float: left; +} diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb new file mode 100644 index 000000000..465194ef9 --- /dev/null +++ b/app/controllers/errors_controller.rb @@ -0,0 +1,15 @@ +class ErrorsController < ApplicationController + layout "error" + + def forbidden + render :status => :forbidden + end + + def not_found + render :status => :not_found + end + + def internal_server_error + render :status => :internal_server_error + end +end diff --git a/app/views/errors/forbidden.html.erb b/app/views/errors/forbidden.html.erb new file mode 100644 index 000000000..4c3fb30d2 --- /dev/null +++ b/app/views/errors/forbidden.html.erb @@ -0,0 +1,3 @@ +

Forbidden

+

The operation you requested on the OpenStreetMap server is only available to administrators (HTTP 403)

+

Feel free to contact the OpenStreetMap community if you have found a broken link / bug. Make a note of the exact URL of your request.

diff --git a/app/views/errors/internal_server_error.html.erb b/app/views/errors/internal_server_error.html.erb new file mode 100644 index 000000000..a2b1ba6f7 --- /dev/null +++ b/app/views/errors/internal_server_error.html.erb @@ -0,0 +1,4 @@ +

Application error

+

The OpenStreetMap server encountered an unexpected condition that prevented it from fulfilling the request (HTTP 500)

+

Feel free to contact the OpenStreetMap community if your problem persists. Make a note of the exact URL / post data of your request.

+

This may be a problem in our Ruby On Rails code. 500 occurs with exceptions thrown outside of an action (like in Dispatcher setups or broken Ruby code)

diff --git a/app/views/errors/not_found.html.erb b/app/views/errors/not_found.html.erb new file mode 100644 index 000000000..6ef39d0d5 --- /dev/null +++ b/app/views/errors/not_found.html.erb @@ -0,0 +1,3 @@ +

File not found

+

Couldn't find a file/directory/API operation by that name on the OpenStreetMap server (HTTP 404)

+

Feel free to contact the OpenStreetMap community if you have found a broken link / bug. Make a note of the exact URL of your request.

diff --git a/app/views/layouts/error.html.erb b/app/views/layouts/error.html.erb new file mode 100644 index 000000000..590f32095 --- /dev/null +++ b/app/views/layouts/error.html.erb @@ -0,0 +1,14 @@ + + + + + OpenStreetMap + <%= stylesheet_link_tag "errors", :media=> "screen" %> + + + <%= image_tag "osm_logo.png", :class => "logo" %> +
+ <%= yield %> +
+ + diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb index cbd93c1d8..5f8a4de5f 100644 --- a/config/initializers/assets.rb +++ b/config/initializers/assets.rb @@ -19,6 +19,7 @@ Rails.application.config.assets.precompile += %w[screen-rtl.css print-rtl.css] Rails.application.config.assets.precompile += %w[leaflet-all.css leaflet.ie.css] Rails.application.config.assets.precompile += %w[id.js id.css] Rails.application.config.assets.precompile += %w[embed.js embed.css] +Rails.application.config.assets.precompile += %w[errors.css] Rails.application.config.assets.precompile += %w[html5shiv.js] Rails.application.config.assets.precompile += %w[images/marker-*.png img/*-handle.png] Rails.application.config.assets.precompile += %w[swfobject.js expressInstall.swf] diff --git a/config/initializers/errors.rb b/config/initializers/errors.rb new file mode 100644 index 000000000..226c6b9b6 --- /dev/null +++ b/config/initializers/errors.rb @@ -0,0 +1 @@ +Rails.application.config.exceptions_app = Rails.application.routes diff --git a/config/routes.rb b/config/routes.rb index 0522115f1..6a3efe3b0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -313,4 +313,9 @@ OpenStreetMap::Application.routes.draw do # redactions resources :redactions + + # errors + match "/403", :to => "errors#forbidden", :via => :all + match "/404", :to => "errors#not_found", :via => :all + match "/500", :to => "errors#internal_server_error", :via => :all end diff --git a/public/403.html b/public/403.html deleted file mode 100644 index 3aab8ccfb..000000000 --- a/public/403.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - -
-

Forbidden

-

The operation you requested on the OpenStreetMap server is only available to administrators (HTTP 403)

-

Feel free to contact the OpenStreetMap community if you have found a broken link / bug. Make a note of the exact URL of your request.

-
- - diff --git a/public/404.html b/public/404.html deleted file mode 100644 index 94eff455f..000000000 --- a/public/404.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - -
-

File not found

-

Couldn't find a file/directory/API operation by that name on the OpenStreetMap server (HTTP 404)

-

Feel free to contact the OpenStreetMap community if you have found a broken link / bug. Make a note of the exact URL of your request.

-
- - diff --git a/public/500.html b/public/500.html deleted file mode 100644 index 1580caa1d..000000000 --- a/public/500.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - -
-

Application error

-

The OpenStreetMap server encountered an unexpected condition that prevented it from fulfilling the request (HTTP 500)

-

Feel free to contact the OpenStreetMap community if your problem persists. Make a note of the exact URL / post data of your request.

-

This may be a problem in our Ruby On Rails code. 500 occurs with exceptions thrown outside of an action (like in Dispatcher setups or broken Ruby code)

-
- - diff --git a/test/controllers/errors_controller_test.rb b/test/controllers/errors_controller_test.rb new file mode 100644 index 000000000..01b07efc8 --- /dev/null +++ b/test/controllers/errors_controller_test.rb @@ -0,0 +1,33 @@ +require "test_helper" + +class ErrorsControllerTest < ActionController::TestCase + def test_routes + assert_routing( + { :path => "/403", :method => :get }, + { :controller => "errors", :action => "forbidden" } + ) + assert_routing( + { :path => "/404", :method => :get }, + { :controller => "errors", :action => "not_found" } + ) + assert_routing( + { :path => "/500", :method => :get }, + { :controller => "errors", :action => "internal_server_error" } + ) + end + + def test_forbidden + get :forbidden + assert_response :forbidden + end + + def test_not_found + get :not_found + assert_response :not_found + end + + def test_internal_server_error + get :internal_server_error + assert_response :internal_server_error + end +end -- 2.39.5