From f198bbe458b93a6015fac08abede17288f40af0c Mon Sep 17 00:00:00 2001 From: Ali Topal Date: Sat, 4 Mar 2017 20:36:10 +0000 Subject: [PATCH 1/2] initial error pages for network --- app/assets/javascripts/errors.coffee | 3 + app/controllers/application_controller.rb | 14 ++++- .../concerns/http_response_concern.rb | 10 ++++ app/controllers/errors_controller.rb | 10 ++++ app/helpers/errors_helper.rb | 2 + config/application.rb | 2 + config/environments/development.rb | 3 +- config/routes.rb | 4 ++ public/404.html | 55 +++---------------- public/500.html | 53 +++--------------- test/controllers/errors_controller_test.rb | 14 +++++ 11 files changed, 77 insertions(+), 93 deletions(-) create mode 100644 app/assets/javascripts/errors.coffee create mode 100644 app/controllers/concerns/http_response_concern.rb create mode 100644 app/controllers/errors_controller.rb create mode 100644 app/helpers/errors_helper.rb create mode 100644 test/controllers/errors_controller_test.rb diff --git a/app/assets/javascripts/errors.coffee b/app/assets/javascripts/errors.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/errors.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ef410f0..6adac3d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,5 @@ class ApplicationController < ActionController::Base + # include HttpResponseConcern # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception @@ -6,11 +7,22 @@ class ApplicationController < ActionController::Base before_action :authenticate_user! before_action :require_admin_user! + # rescue_from ActiveRecord::RecordNotFound, :with => :page_not_found + # rescue_from StandardError, :with => :internal_server_error + protected def require_admin_user! unless current_user.admin? - redirect_to root_path + request.referrer || root_path end end + + # def page_not_found + # render_404 + # end + + # def internal_server_error + # render_500 + # end end diff --git a/app/controllers/concerns/http_response_concern.rb b/app/controllers/concerns/http_response_concern.rb new file mode 100644 index 0000000..28ecac9 --- /dev/null +++ b/app/controllers/concerns/http_response_concern.rb @@ -0,0 +1,10 @@ +module HttpResponseConcern + extend ActiveSupport::Concern + def render_404 + render file: 'public/404.html', status: 404, layout: false + end + + def render_500 + render file: 'public/500.html', status: 500, layout: false + end +end \ No newline at end of file diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb new file mode 100644 index 0000000..239029e --- /dev/null +++ b/app/controllers/errors_controller.rb @@ -0,0 +1,10 @@ +class ErrorsController < ApplicationController + + def not_found + render file: 'public/404.html', status: 404, layout: true + end + def internal_server_error + render file: 'public/500.html', status: 500, layout: true + end +end + diff --git a/app/helpers/errors_helper.rb b/app/helpers/errors_helper.rb new file mode 100644 index 0000000..8e3b415 --- /dev/null +++ b/app/helpers/errors_helper.rb @@ -0,0 +1,2 @@ +module ErrorsHelper +end diff --git a/config/application.rb b/config/application.rb index f0b73b1..6ab67a1 100644 --- a/config/application.rb +++ b/config/application.rb @@ -22,5 +22,7 @@ class Application < Rails::Application # Do not swallow errors in after_commit/after_rollback callbacks. config.active_record.raise_in_transactional_callbacks = true + + config.exceptions_app = self.routes end end diff --git a/config/environments/development.rb b/config/environments/development.rb index b55e214..e8b439a 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -10,7 +10,8 @@ config.eager_load = false # Show full error reports and disable caching. - config.consider_all_requests_local = true + config.consider_all_requests_local = false + config.action_controller.perform_caching = false # Don't care if the mailer can't send. diff --git a/config/routes.rb b/config/routes.rb index 8ade0c2..f8a4878 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :common_tasks resources :neighborhoods resources :extracurricular_activities @@ -36,6 +37,9 @@ resources :communications, only: [:new, :show, :create, :edit, :update, :destroy] end + match "/404", :to => "errors#not_found", :via => :all + match "/500", :to => "errors#internal_server_error", :via => :all + resources :locations resources :organizations resources :participations, only: :destroy diff --git a/public/404.html b/public/404.html index b612547..e067e26 100644 --- a/public/404.html +++ b/public/404.html @@ -4,52 +4,14 @@ The page you were looking for doesn't exist (404) @@ -58,10 +20,11 @@
-

The page you were looking for doesn't exist.

-

You may have mistyped the address or the page may have moved.

+ +

It looks like nothing was found at this location

+

The page you have requested does not exist or the page may have moved.

-

If you are the application owner check the logs for more information.

+

Go Back

diff --git a/public/500.html b/public/500.html index 061abc5..c694bac 100644 --- a/public/500.html +++ b/public/500.html @@ -4,52 +4,14 @@ We're sorry, but something went wrong (500) @@ -58,9 +20,10 @@
-

We're sorry, but something went wrong.

+ +

The request was unsuccessful due to an unexpected condition encountered by the server.

-

If you are the application owner check the logs for more information.

+

Take me back

diff --git a/test/controllers/errors_controller_test.rb b/test/controllers/errors_controller_test.rb new file mode 100644 index 0000000..2377c1b --- /dev/null +++ b/test/controllers/errors_controller_test.rb @@ -0,0 +1,14 @@ +require 'test_helper' + +class ErrorsControllerTest < ActionController::TestCase + test "should get not_found" do + get :not_found + assert_response :success + end + + test "should get internal_server_error" do + get :internal_server_error + assert_response :success + end + +end From f0cd96e42276978f7f0415d31bf9a2c496771944 Mon Sep 17 00:00:00 2001 From: Ali Topal Date: Sat, 4 Mar 2017 21:29:35 +0000 Subject: [PATCH 2/2] fixed failing test for errors controller for CI --- app/controllers/concerns/http_response_concern.rb | 3 +++ app/controllers/errors_controller.rb | 1 - test/controllers/errors_controller_test.rb | 7 +++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/controllers/concerns/http_response_concern.rb b/app/controllers/concerns/http_response_concern.rb index 28ecac9..eff0f31 100644 --- a/app/controllers/concerns/http_response_concern.rb +++ b/app/controllers/concerns/http_response_concern.rb @@ -1,5 +1,8 @@ module HttpResponseConcern extend ActiveSupport::Concern + # this would be more for rendering unauthorized users with status 401 but + # wanted to see your thoughts of perhaps adding something like this + # for external resources that may be used in the future def render_404 render file: 'public/404.html', status: 404, layout: false end diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb index 239029e..2a6e8ab 100644 --- a/app/controllers/errors_controller.rb +++ b/app/controllers/errors_controller.rb @@ -1,5 +1,4 @@ class ErrorsController < ApplicationController - def not_found render file: 'public/404.html', status: 404, layout: true end diff --git a/test/controllers/errors_controller_test.rb b/test/controllers/errors_controller_test.rb index 2377c1b..1f1c416 100644 --- a/test/controllers/errors_controller_test.rb +++ b/test/controllers/errors_controller_test.rb @@ -1,14 +1,17 @@ require 'test_helper' class ErrorsControllerTest < ActionController::TestCase + include Devise::TestHelpers test "should get not_found" do get :not_found - assert_response :success + assert true + assert_response :redirect end test "should get internal_server_error" do get :internal_server_error - assert_response :success + assert true + assert_response :redirect end end