From 9efa9d5d43f887d323def01a19c43531f407354b Mon Sep 17 00:00:00 2001 From: wvengen Date: Sat, 6 Jun 2015 20:20:07 +0200 Subject: [PATCH] Handle errors using Gaffe --- Gemfile | 1 + Gemfile.lock | 3 +++ .../bootstrap_and_overrides.css.less | 9 +++++++ app/controllers/errors_controller.rb | 24 +++++++++++++++++++ app/views/errors/_error.html.haml | 9 +++++++ .../errors/internal_server_error.html.haml | 5 ++++ app/views/errors/not_found.html.haml | 4 ++++ app/views/finance/invoices/new.html.haml | 2 +- app/views/finance/invoices/show.html.haml | 3 +-- config/initializers/gaffe.rb | 6 +++++ config/locales/en.yml | 10 ++++++-- public/404.html | 12 ---------- 12 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 app/controllers/errors_controller.rb create mode 100644 app/views/errors/_error.html.haml create mode 100644 app/views/errors/internal_server_error.html.haml create mode 100644 app/views/errors/not_found.html.haml create mode 100644 config/initializers/gaffe.rb delete mode 100644 public/404.html diff --git a/Gemfile b/Gemfile index 023aea0e..0c7668b3 100644 --- a/Gemfile +++ b/Gemfile @@ -46,6 +46,7 @@ gem 'recurring_select' gem 'roo', '~> 2.0.0' gem 'roo-xls' gem 'spreadsheet' +gem 'gaffe' # we use the git version of acts_as_versioned, and need to include it in this Gemfile gem 'acts_as_versioned', github: 'technoweenie/acts_as_versioned' diff --git a/Gemfile.lock b/Gemfile.lock index b473f907..2a22872e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -153,6 +153,8 @@ GEM faker (1.4.3) i18n (~> 0.5) ffi (1.9.10) + gaffe (1.0.2) + rails (>= 3.2.0) git-version-bump (0.15.1) globalid (0.3.5) activesupport (>= 4.1.0) @@ -488,6 +490,7 @@ DEPENDENCIES faker foodsoft_messages! foodsoft_wiki! + gaffe haml-rails i18n-js (~> 3.0.0.rc8) i18n-spec diff --git a/app/assets/stylesheets/bootstrap_and_overrides.css.less b/app/assets/stylesheets/bootstrap_and_overrides.css.less index bb8ccc3d..1911eeae 100644 --- a/app/assets/stylesheets/bootstrap_and_overrides.css.less +++ b/app/assets/stylesheets/bootstrap_and_overrides.css.less @@ -160,6 +160,15 @@ table { } } +// Error page +.large-error-icon { + float: left; + text-align: right; + font-size: 160px; + color: lightgrey; + margin-left: 25px; +} + // Tasks .. .accepted { color: #468847; diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb new file mode 100644 index 00000000..ed0c2583 --- /dev/null +++ b/app/controllers/errors_controller.rb @@ -0,0 +1,24 @@ +class ErrorsController < ApplicationController + include Gaffe::Errors + + skip_before_filter :authenticate + + layout :current_layout + + def show + render "errors/#{@rescue_response}", status: @status_code + end + + private + + def current_layout + # Need foodcoop for `current_user`, even though it may not be retrieved from the url. + params[:foodcoop] ||= session[:scope] + current_user ? 'application' : 'login' + end + + def login_layout? + current_user.nil? + end + helper_method :login_layout? +end diff --git a/app/views/errors/_error.html.haml b/app/views/errors/_error.html.haml new file mode 100644 index 00000000..bcd1b2ad --- /dev/null +++ b/app/views/errors/_error.html.haml @@ -0,0 +1,9 @@ +- title local_assigns[:title], false + +- unless login_layout? + %i.hidden-phone.large-error-icon.icon-exclamation-sign +%div(class="#{login_layout? ? '' : 'span6'}") + %h1= local_assigns[:title] + %div(class='alert alert-#{local_assigns[:type] || 'warn'}') + = local_assigns[:page] + = link_to t('ui.back'), 'javascript:history.go(-1)', class: 'btn btn-primary' 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 00000000..7a598e3d --- /dev/null +++ b/app/views/errors/internal_server_error.html.haml @@ -0,0 +1,5 @@ +- page = capture do + %p= t '.text1' + %p= t '.text2' + += render 'error', title: t('.title'), type: :error, page: page diff --git a/app/views/errors/not_found.html.haml b/app/views/errors/not_found.html.haml new file mode 100644 index 00000000..d0f32754 --- /dev/null +++ b/app/views/errors/not_found.html.haml @@ -0,0 +1,4 @@ +- page = capture do + %p= t '.text' + += render 'error', title: t('.title'), page: page diff --git a/app/views/finance/invoices/new.html.haml b/app/views/finance/invoices/new.html.haml index 375bc917..f7e56474 100644 --- a/app/views/finance/invoices/new.html.haml +++ b/app/views/finance/invoices/new.html.haml @@ -1,3 +1,3 @@ - title t('.title') = render :partial => 'form' -= link_to t('.back'), finance_invoices_path += link_to t('ui.or_cancel'), finance_invoices_path diff --git a/app/views/finance/invoices/show.html.haml b/app/views/finance/invoices/show.html.haml index 1106aa5c..5f202e3a 100644 --- a/app/views/finance/invoices/show.html.haml +++ b/app/views/finance/invoices/show.html.haml @@ -36,5 +36,4 @@ =h @invoice.note = link_to t('ui.edit'), edit_finance_invoice_path(@invoice) -| -= link_to t('.back'), finance_invoices_path += link_to t('ui.or_cancel'), finance_invoices_path diff --git a/config/initializers/gaffe.rb b/config/initializers/gaffe.rb new file mode 100644 index 00000000..a77ce214 --- /dev/null +++ b/config/initializers/gaffe.rb @@ -0,0 +1,6 @@ +if Rails.env.production? || Rails.env.staging? || true + Gaffe.configure do |config| + config.errors_controller = 'ErrorsController' + end + Gaffe.enable! +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 4e1d75c0..544088d2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -638,6 +638,13 @@ en: general: A problem has occured. general_again: A problem has occured. Please try again. general_msg: 'A problem has occured: %{msg}' + not_found: + title: Page not found + text: This page does not appear to exist, sorry! + internal_server_error: + title: Internal server error + text1: An unexpected error has occured. Sorry! + text2: We've been notified. If this remains a problem, please tell us so. feedback: create: notice: Your feedback was sent successfully. Thanks a lot! @@ -771,10 +778,8 @@ en: linked_delivery: a delivery linked_order: an order new: - back: Back title: Create new invoice show: - back: Back title: Invoice %{number} ordergroups: index: @@ -1685,6 +1690,7 @@ en: title_all: All group tasks ui: actions: Actions + back: Back cancel: Cancel close: Close copy: Copy diff --git a/public/404.html b/public/404.html deleted file mode 100644 index 3bd89127..00000000 --- a/public/404.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - -

Seite/Datei nicht gefunden!

-

Das ist wohl eine Sackgasse und sollte eigentlich nicht vorkommen.

-

Bitte melden unter foodsoft [ätt] foodcoops.net

- -