Skip to content
This repository was archived by the owner on Jun 21, 2021. It is now read-only.

Commit 2cea6a6

Browse files
committed
refactor error handling
1 parent 7c392ca commit 2cea6a6

File tree

9 files changed

+84
-90
lines changed

9 files changed

+84
-90
lines changed

app/controllers/application_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
class ApplicationController < ActionController::Base
2+
include ControllerErrorHandling
3+
24
protect_from_forgery
35

46
after_action :set_content_type_to_xhtml, :tidy_response_body
57
helper_method :logged_in?, :current_user
68
helper_method :authorized_to_show_user?, :authorized_to_show_user_best?
79

10+
def routing_error
11+
raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}"
12+
end
13+
814
protected
915
def logged_in?
1016
!!session[:user_id]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module ControllerErrorHandling
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
rescue_from StandardError do |exception|
6+
@message = "#{t("error.internal_error")}: #{exception.class}"
7+
render "shared/common_error", status: 500, formats: :html
8+
end
9+
10+
rescue_from Aclog::Exceptions::Forbidden do |exception|
11+
@message = t("error.forbidden")
12+
render "shared/common_error", status: 403, formats: :html
13+
end
14+
15+
rescue_from Aclog::Exceptions::UserProtected do |exception|
16+
@message = t("error.forbidden")
17+
render "shared/user_forbidden_error", status: 403, formats: :html
18+
end
19+
20+
rescue_from ActionController::RoutingError, ActiveRecord::RecordNotFound, Aclog::Exceptions::NotFound do |exception|
21+
@message = t("error.not_found")
22+
render "shared/common_error", status: 404, formats: :html
23+
end
24+
end
25+
end

app/controllers/errors_controller.rb

Lines changed: 0 additions & 37 deletions
This file was deleted.

app/views/errors/render_error.html.haml

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- title "#{response.status}: #{@message}"
2+
.container.error-page
3+
%h1= response.status
4+
%p= @message
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- title "#{response.status}: #{@message}"
2+
.col-sm-3.col-md-offset-1
3+
= render "shared/sidebar_user", user: @user
4+
.col-sm-9.col-md-7.col-lg-6.error-page
5+
%h1= response.status
6+
%p= @message

config/application.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ class Application < Rails::Application
3434
config.i18n.default_locale = :ja
3535
config.i18n.fallbacks = true
3636

37-
config.exceptions_app = -> env do
38-
ErrorsController.action(:render_error).call(env)
39-
end
40-
4137
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif *.eot *.woff *.ttf *.svg)
4238
config.assets.precompile += [Proc.new { |path, fn| fn =~ /app\/assets/ && %w(.js).include?(File.extname(path)) }]
4339

config/locales/ja.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ ja:
22
error:
33
not_found: このページは存在しません
44
forbidden: このページを表示する権限がありません
5+
unauthorized: このページを表示する権限がありません
56
internal_error: エラー
67
views:
78
settings:

config/routes.rb

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,49 @@
33

44
mount Api => "/api"
55

6-
# Internals / SessionsController
7-
get "/i/callback" => "sessions#create"
8-
get "/i/logout" => "sessions#destroy", as: "logout"
9-
10-
get "/i/user_jump_suggest" => "users#user_jump_suggest", as: "user_jump_suggest"
11-
12-
get "/i/:id" => "tweets#show", as: "tweet", constraints: { id: /\d+/ }
13-
get "/i/:id/import" => "tweets#import", as: "import", constraints: { id: /\d+/ }
14-
15-
get "/i/settings" => "settings#index", as: "settings"
16-
post "/i/settings/update" => "settings#update"
17-
get "/i/settings/confirm_deactivation" => "settings#confirm_deactivation"
18-
post "/i/settings/deactivate" => "settings#deactivate"
19-
20-
get "/i/best" => "tweets#all_best", as: "best"
21-
get "/i/recent" => "tweets#all_recent", as: "recent"
22-
get "/i/timeline" => "tweets#all_timeline", as: "timeline"
23-
get "/i/filter" => "tweets#filter", as: "filter"
24-
25-
get "/about/api" => "apidocs#index", as: "about_api"
26-
get "/about/api/:method/:namespace/:path" => "apidocs#endpoint", as: "about_api_endpoint", constraints: { namespace: /[\w\/]+/ }
27-
28-
# User pages
29-
scope "/:screen_name" do
30-
get "/" => "tweets#user_index", as: "user"
31-
get "/best" => "tweets#user_best", as: "user_best"
32-
get "/recent" => "tweets#user_recent", as: "user_recent"
33-
get "/timeline" => "tweets#user_timeline", as: "user_timeline"
34-
get "/discoveries" => "tweets#user_discoveries", as: "user_discoveries"
35-
get "/favorites" => "tweets#user_favorites", as: "user_favorites"
36-
get "/retweets" => "tweets#user_retweets", as: "user_retweets"
37-
get "/discovered_by/:source_screen_name" => "tweets#user_discovered_by", as: "user_discovered_by_user"
38-
39-
get "/discovered_by" => "users#discovered_by", as: "user_discovered_by"
40-
get "/discovered_users" => "users#discovered_users", as: "user_discovered_users"
41-
get "/stats" => "users#stats", as: "user_stats"
6+
constraints format: :html do
7+
# Internals / SessionsController
8+
get "/i/callback" => "sessions#create"
9+
get "/i/logout" => "sessions#destroy", as: "logout"
10+
11+
get "/i/user_jump_suggest" => "users#user_jump_suggest", as: "user_jump_suggest"
12+
13+
get "/i/:id" => "tweets#show", as: "tweet", constraints: { id: /\d+/ }
14+
get "/i/:id/import" => "tweets#import", as: "import", constraints: { id: /\d+/ }
15+
16+
get "/i/settings" => "settings#index", as: "settings"
17+
post "/i/settings/update" => "settings#update"
18+
get "/i/settings/confirm_deactivation" => "settings#confirm_deactivation"
19+
post "/i/settings/deactivate" => "settings#deactivate"
20+
21+
get "/i/best" => "tweets#all_best", as: "best"
22+
get "/i/recent" => "tweets#all_recent", as: "recent"
23+
get "/i/timeline" => "tweets#all_timeline", as: "timeline"
24+
get "/i/filter" => "tweets#filter", as: "filter"
25+
26+
get "/about/api" => "apidocs#index", as: "about_api"
27+
get "/about/api/:method/:namespace/:path" => "apidocs#endpoint", as: "about_api_endpoint", constraints: { namespace: /[\w\/]+/ }
28+
29+
# User pages
30+
scope "/:screen_name" do
31+
get "/" => "tweets#user_index", as: "user"
32+
get "/best" => "tweets#user_best", as: "user_best"
33+
get "/recent" => "tweets#user_recent", as: "user_recent"
34+
get "/timeline" => "tweets#user_timeline", as: "user_timeline"
35+
get "/discoveries" => "tweets#user_discoveries", as: "user_discoveries"
36+
get "/favorites" => "tweets#user_favorites", as: "user_favorites"
37+
get "/retweets" => "tweets#user_retweets", as: "user_retweets"
38+
get "/discovered_by/:source_screen_name" => "tweets#user_discovered_by", as: "user_discovered_by_user"
39+
40+
get "/discovered_by" => "users#discovered_by", as: "user_discovered_by"
41+
get "/discovered_users" => "users#discovered_users", as: "user_discovered_users"
42+
get "/stats" => "users#stats", as: "user_stats"
43+
end
44+
45+
# Twitter redirect
46+
get "/:screen_name/status(es)/:id" => redirect("/i/%{id}")
4247
end
4348

44-
# Twitter redirect
45-
get "/:screen_name/status(es)/:id" => redirect("/i/%{id}")
49+
get "*unmatched_route" => "application#routing_error"
4650
end
4751

0 commit comments

Comments
 (0)