]> git.openstreetmap.org Git - rails.git/commitdiff
Use dynamic error pages built through the asset pipeline
authorTom Hughes <tom@compton.nu>
Wed, 1 Aug 2018 17:56:11 +0000 (18:56 +0100)
committerTom Hughes <tom@compton.nu>
Wed, 1 Aug 2018 18:13:04 +0000 (19:13 +0100)
Fixes #1241

14 files changed:
.rubocop_todo.yml
app/assets/stylesheets/errors.scss [new file with mode: 0644]
app/controllers/errors_controller.rb [new file with mode: 0644]
app/views/errors/forbidden.html.erb [new file with mode: 0644]
app/views/errors/internal_server_error.html.erb [new file with mode: 0644]
app/views/errors/not_found.html.erb [new file with mode: 0644]
app/views/layouts/error.html.erb [new file with mode: 0644]
config/initializers/assets.rb
config/initializers/errors.rb [new file with mode: 0644]
config/routes.rb
public/403.html [deleted file]
public/404.html [deleted file]
public/500.html [deleted file]
test/controllers/errors_controller_test.rb [new file with mode: 0644]

index a64be72735cff92ec5b445ba875bdd05f5660461..f012237b31aba8b6fff09b291fa9f98ce279c6b4 100644 (file)
@@ -54,7 +54,7 @@ Metrics/AbcSize:
 # Offense count: 41
 # Configuration parameters: CountComments, ExcludedMethods.
 Metrics/BlockLength:
 # Offense count: 41
 # Configuration parameters: CountComments, ExcludedMethods.
 Metrics/BlockLength:
-  Max: 258
+  Max: 261
 
 # Offense count: 11
 # Configuration parameters: CountBlocks.
 
 # Offense count: 11
 # Configuration parameters: CountBlocks.
diff --git a/app/assets/stylesheets/errors.scss b/app/assets/stylesheets/errors.scss
new file mode 100644 (file)
index 0000000..fd14002
--- /dev/null
@@ -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 (file)
index 0000000..465194e
--- /dev/null
@@ -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 (file)
index 0000000..4c3fb30
--- /dev/null
@@ -0,0 +1,3 @@
+<h1>Forbidden</h1>  
+<p>The operation you requested on the OpenStreetMap server is only available to administrators (HTTP 403)</p>
+<p>Feel free to <a href="http://wiki.openstreetmap.org/wiki/Contact" title="Various contact channels explained">contact</a> the OpenStreetMap community if you have found a broken link / bug. Make a note of the exact URL of your request.</p>
diff --git a/app/views/errors/internal_server_error.html.erb b/app/views/errors/internal_server_error.html.erb
new file mode 100644 (file)
index 0000000..a2b1ba6
--- /dev/null
@@ -0,0 +1,4 @@
+<h1>Application error</h1>
+<p>The OpenStreetMap server encountered an unexpected condition that prevented it from fulfilling the request (HTTP 500)</p>
+<p>Feel free to <a href="http://wiki.openstreetmap.org/wiki/Contact" title="Various contact channels explained">contact</a> the OpenStreetMap community if your problem persists. Make a note of the exact URL / post data of your request.</p>
+<p>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)</p>
diff --git a/app/views/errors/not_found.html.erb b/app/views/errors/not_found.html.erb
new file mode 100644 (file)
index 0000000..6ef39d0
--- /dev/null
@@ -0,0 +1,3 @@
+<h1>File not found</h1>  
+<p>Couldn't find a file/directory/API operation by that name on the OpenStreetMap server (HTTP 404)</p>
+<p>Feel free to <a href="http://wiki.openstreetmap.org/wiki/Contact" title="Various contact channels explained">contact</a> the OpenStreetMap community if you have found a broken link / bug. Make a note of the exact URL of your request.</p>
diff --git a/app/views/layouts/error.html.erb b/app/views/layouts/error.html.erb
new file mode 100644 (file)
index 0000000..590f320
--- /dev/null
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta charset="utf-8">
+    <title>OpenStreetMap</title>
+    <%= stylesheet_link_tag "errors", :media=> "screen" %>
+  </head>
+  <body>
+    <%= image_tag "osm_logo.png", :class => "logo" %>
+    <div class="details">
+      <%= yield %>
+    </div>
+  </body>
+</html>
index cbd93c1d8d38bdab1c94bb96fea848ae58ac4621..5f8a4de5ff6d792bd18edef0fad024506093bc28 100644 (file)
@@ -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[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]
 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 (file)
index 0000000..226c6b9
--- /dev/null
@@ -0,0 +1 @@
+Rails.application.config.exceptions_app = Rails.application.routes
index 0522115f1b0d85bc7ecdfc81cf3dc10a32cd7d24..6a3efe3b05674370850163e370c9ccade1452c1b 100644 (file)
@@ -313,4 +313,9 @@ OpenStreetMap::Application.routes.draw do
 
   # redactions
   resources :redactions
 
   # 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
 end
diff --git a/public/403.html b/public/403.html
deleted file mode 100644 (file)
index 3aab8cc..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
-   "http://www.w3.org/TR/html4/loose.dtd">
-<html>
-<body>
-  <img src="/assets/osm_logo.png" style="float:left; margin:10px">
-  <div style="float:left;">
-    <h1>Forbidden</h1>  
-    <p>The operation you requested on the OpenStreetMap server is only available to administrators (HTTP 403)</p>
-    <p>Feel free to <a href="http://wiki.openstreetmap.org/wiki/Contact" title="Various contact channels explained">contact</a> the OpenStreetMap community if you have found a broken link / bug. Make a note of the exact URL of your request.</p>
-  </div>
-</body>
-</html>
diff --git a/public/404.html b/public/404.html
deleted file mode 100644 (file)
index 94eff45..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
-   "http://www.w3.org/TR/html4/loose.dtd">
-<html>
-<body>
-  <img src="/assets/osm_logo.png" style="float:left; margin:10px">
-  <div style="float:left;">
-    <h1>File not found</h1>  
-    <p>Couldn't find a file/directory/API operation by that name on the OpenStreetMap server (HTTP 404)</p>
-    <p>Feel free to <a href="http://wiki.openstreetmap.org/wiki/Contact" title="Various contact channels explained">contact</a> the OpenStreetMap community if you have found a broken link / bug. Make a note of the exact URL of your request.</p>
-  </div>
-</body>
-</html>
diff --git a/public/500.html b/public/500.html
deleted file mode 100644 (file)
index 1580caa..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
-   "http://www.w3.org/TR/html4/loose.dtd">
-<html>
-<body>
-  <img src="/assets/osm_logo.png" style="float:left; margin:10px">
-  <div style="float:left;">
-    <h1>Application error</h1>
-    <p>The OpenStreetMap server encountered an unexpected condition that prevented it from fulfilling the request (HTTP 500)</p>
-    <p>Feel free to <a href="http://wiki.openstreetmap.org/wiki/Contact" title="Various contact channels explained">contact</a> the OpenStreetMap community if your problem persists. Make a note of the exact URL / post data of your request.</p>
-    <p>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)</p>
-  </div>
-</body>
-</html>
diff --git a/test/controllers/errors_controller_test.rb b/test/controllers/errors_controller_test.rb
new file mode 100644 (file)
index 0000000..01b07ef
--- /dev/null
@@ -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