aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorRhenium <rhenium@rhe.jp>2014-03-13 18:51:49 +0900
committerRhenium <rhenium@rhe.jp>2014-03-13 18:51:49 +0900
commit2cea6a60d9e0e2fc90199084e5e99395e1b02964 (patch)
tree451459585087da621de963f72283e29863d47995 /app
parent7c392ca7735a36948515a392b544ace60a242838 (diff)
downloadaclog-2cea6a60d9e0e2fc90199084e5e99395e1b02964.tar.gz
refactor error handling
Diffstat (limited to 'app')
-rw-r--r--app/controllers/application_controller.rb6
-rw-r--r--app/controllers/concerns/controller_error_handling.rb25
-rw-r--r--app/controllers/errors_controller.rb37
-rw-r--r--app/views/errors/render_error.html.haml11
-rw-r--r--app/views/shared/common_error.html.haml4
-rw-r--r--app/views/shared/user_forbidden_error.html.haml6
6 files changed, 41 insertions, 48 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 66eb4ca..eb27964 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -1,10 +1,16 @@
class ApplicationController < ActionController::Base
+ include ControllerErrorHandling
+
protect_from_forgery
after_action :set_content_type_to_xhtml, :tidy_response_body
helper_method :logged_in?, :current_user
helper_method :authorized_to_show_user?, :authorized_to_show_user_best?
+ def routing_error
+ raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}"
+ end
+
protected
def logged_in?
!!session[:user_id]
diff --git a/app/controllers/concerns/controller_error_handling.rb b/app/controllers/concerns/controller_error_handling.rb
new file mode 100644
index 0000000..bcc1f48
--- /dev/null
+++ b/app/controllers/concerns/controller_error_handling.rb
@@ -0,0 +1,25 @@
+module ControllerErrorHandling
+ extend ActiveSupport::Concern
+
+ included do
+ rescue_from StandardError do |exception|
+ @message = "#{t("error.internal_error")}: #{exception.class}"
+ render "shared/common_error", status: 500, formats: :html
+ end
+
+ rescue_from Aclog::Exceptions::Forbidden do |exception|
+ @message = t("error.forbidden")
+ render "shared/common_error", status: 403, formats: :html
+ end
+
+ rescue_from Aclog::Exceptions::UserProtected do |exception|
+ @message = t("error.forbidden")
+ render "shared/user_forbidden_error", status: 403, formats: :html
+ end
+
+ rescue_from ActionController::RoutingError, ActiveRecord::RecordNotFound, Aclog::Exceptions::NotFound do |exception|
+ @message = t("error.not_found")
+ render "shared/common_error", status: 404, formats: :html
+ end
+ end
+end
diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb
deleted file mode 100644
index f59c6e5..0000000
--- a/app/controllers/errors_controller.rb
+++ /dev/null
@@ -1,37 +0,0 @@
-class ErrorsController < ApplicationController
- before_action :force_format
-
- def render_error
- @exception = env["action_dispatch.exception"]
-
- case @exception
- when Aclog::Exceptions::Forbidden
- @status = 403
- @message = t("error.forbidden")
- when ActionController::RoutingError,
- ActiveRecord::RecordNotFound,
- ActionView::MissingTemplate,
- Aclog::Exceptions::NotFound
- @status = 404
- @message = t("error.not_found")
- when OAuth::Unauthorized,
- Aclog::Exceptions::Unauthorized
- @status = 401
- @message = ""
- else
- @status = 500
- @message = "#{t("error.internal_error")}: #{@exception.class}"
- end
-
- render status: @status
- end
-
- private
- def force_format
- request.format = (env["REQUEST_PATH"].scan(/\.([A-Za-z]+)$/).flatten.first || :html).to_sym
-
- unless request.format == :html || request.format == :json
- request.format = :html
- end
- end
-end
diff --git a/app/views/errors/render_error.html.haml b/app/views/errors/render_error.html.haml
deleted file mode 100644
index 3bd94c8..0000000
--- a/app/views/errors/render_error.html.haml
+++ /dev/null
@@ -1,11 +0,0 @@
-- title "#{response.status}: @message"
-- if @user
- .col-sm-3.col-md-offset-1
- = render "shared/sidebar_user", user: @user
- .col-sm-9.col-md-7.col-lg-6.error-page
- %h1= response.status
- %p= @message
-- else
- .container.error-page
- %h1= response.status
- %p= @message
diff --git a/app/views/shared/common_error.html.haml b/app/views/shared/common_error.html.haml
new file mode 100644
index 0000000..f5dcc29
--- /dev/null
+++ b/app/views/shared/common_error.html.haml
@@ -0,0 +1,4 @@
+- title "#{response.status}: #{@message}"
+.container.error-page
+ %h1= response.status
+ %p= @message
diff --git a/app/views/shared/user_forbidden_error.html.haml b/app/views/shared/user_forbidden_error.html.haml
new file mode 100644
index 0000000..0a09b75
--- /dev/null
+++ b/app/views/shared/user_forbidden_error.html.haml
@@ -0,0 +1,6 @@
+- title "#{response.status}: #{@message}"
+.col-sm-3.col-md-offset-1
+ = render "shared/sidebar_user", user: @user
+.col-sm-9.col-md-7.col-lg-6.error-page
+ %h1= response.status
+ %p= @message