diff options
author | rhenium <re4k@re4k.info> | 2013-05-30 17:22:55 +0900 |
---|---|---|
committer | rhenium <re4k@re4k.info> | 2013-05-30 17:22:55 +0900 |
commit | 72fb7942e6ea02b7182a3442175332009d1dac14 (patch) | |
tree | 7792addac50616724da96feafe433f9e0536785c | |
parent | 75e30f750533af57057fd5a9b7f725218f4be7d0 (diff) | |
download | aclog-72fb7942e6ea02b7182a3442175332009d1dac14.tar.gz |
improve error pages
-rw-r--r-- | app/controllers/application_controller.rb | 6 | ||||
-rw-r--r-- | app/controllers/errors_controller.rb | 33 | ||||
-rw-r--r-- | app/controllers/tweets_controller.rb | 2 | ||||
-rw-r--r-- | app/models/user.rb | 2 | ||||
-rw-r--r-- | app/views/shared/sidebar/_users.html.haml | 3 | ||||
-rw-r--r-- | lib/aclog/exceptions.rb | 14 |
6 files changed, 39 insertions, 21 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index bd7112a..085baf1 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -45,13 +45,13 @@ class ApplicationController < ActionController::Base end def authorize_to_show_user!(user) - authorized_to_show_user?(user) or raise Aclog::Exceptions::UserProtected + authorized_to_show_user?(user) or raise Aclog::Exceptions::UserProtected.new(user) end def authorize_to_show_best!(user) authorize_to_show_user!(user) - raise Aclog::Exceptions::UserNotRegistered unless user.registered? - raise Aclog::Exceptions::AccountPrivate if user.account.private? && user.id != session[:user_id] + raise Aclog::Exceptions::UserNotRegistered.new(user) unless user.registered? + raise Aclog::Exceptions::AccountPrivate.new(user) if user.account.private? && user.id != session[:user_id] true end diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb index 1a7eed9..08d7f82 100644 --- a/app/controllers/errors_controller.rb +++ b/app/controllers/errors_controller.rb @@ -1,8 +1,8 @@ # -*- encoding: utf-8 -*- class ErrorsController < ApplicationController - layout "index" skip_before_filter :check_format before_filter :force_format + layout :select_layout def render_error @exception = env["action_dispatch.exception"] @@ -19,33 +19,44 @@ class ErrorsController < ApplicationController when Aclog::Exceptions::UserNotFound @status = 404 @message = "ユーザーが見つかりませんでした。" - when Aclog::Exceptions::UserNotRegistered - @status = 404 - @message = "ユーザーは aclog に登録していません。" - when Aclog::Exceptions::UserProtected - @status = 403 - @message = "ユーザーは非公開です。" when Aclog::Exceptions::LoginRequired @status = 403 @message = "このページの表示にはログインが必要です。" when Aclog::Exceptions::OAuthEchoUnauthorized @status = 401 @message = "OAuth Echo 認証に失敗しました。" - when Aclog::Exceptions::AccountPrivate - @status = 403 - @message = "ユーザーの best は非公開です" when ActionController::RoutingError @status = 404 @message = "このページは存在しません。" + when Aclog::Exceptions::UserNotRegistered + @status = 404 + @message = "ユーザーは aclog に登録していません。" + when Aclog::Exceptions::UserProtected + @status = 403 + @message = "ユーザーは非公開です。" + when Aclog::Exceptions::AccountPrivate + @status = 403 + @message = "ユーザーの best は非公開です" else @status = 500 @message = "Internal Error: #{@exception.class}" end - render status: @status + if @exception.is_a? Aclog::Exceptions::UserError + @user = @exception.user + end + + respond_to do |format| + format.html { render status: @status } + format.json { render status: @status } + end end private + def select_layout + @user ? nil : "index" + end + def force_format if request.format == :html request.format = (env["REQUEST_PATH"].scan(/\.([A-Za-z]+)$/).flatten.first || :html).to_sym diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index b252406..8879289 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -137,7 +137,7 @@ class TweetsController < ApplicationController def _require_user(user_id, screen_name) user = _get_user(user_id, screen_name) raise Aclog::Exceptions::UserNotFound unless user - raise Aclog::Exceptions::UserProtected unless authorized_to_show_user?(user) + raise Aclog::Exceptions::UserProtected.new(user) unless authorized_to_show_user?(user) user end end diff --git a/app/models/user.rb b/app/models/user.rb index ac35061..c61a5fe 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -56,7 +56,7 @@ class User < ActiveRecord::Base end def stats - raise Aclog::Exceptions::UserNotRegistered unless registered? + raise Aclog::Exceptions::UserNotRegistered.new(self) unless registered? Rails.cache.fetch("stats/#{self.id}", expires_in: 3.hours) do favorited_counts, retweeted_counts = self.tweets.pluck(:favorites_count, :retweets_count).transpose diff --git a/app/views/shared/sidebar/_users.html.haml b/app/views/shared/sidebar/_users.html.haml index dba837e..6559641 100644 --- a/app/views/shared/sidebar/_users.html.haml +++ b/app/views/shared/sidebar/_users.html.haml @@ -33,12 +33,11 @@ = link_to "discovered by", user_discovered_by_path(@user.screen_name) %li = link_to "discovered users", user_discovered_users_path(@user.screen_name) - %li - = link_to "stats", user_stats_path(@user.screen_name) - else %li = link_to "timeline", user_path(@user.screen_name) %li = link_to "discoveries", user_discoveries_path(@user.screen_name) + - if @user.registered? %li = link_to "stats", user_stats_path(@user.screen_name) diff --git a/lib/aclog/exceptions.rb b/lib/aclog/exceptions.rb index befcbde..e9ea212 100644 --- a/lib/aclog/exceptions.rb +++ b/lib/aclog/exceptions.rb @@ -1,11 +1,19 @@ module Aclog module Exceptions + class UserError < StandardError + attr_reader :user + def initialize(user) + @user = user + end + end + class UserNotFound < StandardError; end - class UserNotRegistered < StandardError; end - class UserProtected < StandardError; end class LoginRequired < StandardError; end class TweetNotFound < StandardError; end - class AccountPrivate < StandardError; end class OAuthEchoUnauthorized < StandardError; end + + class UserNotRegistered < UserError; end + class UserProtected < UserError; end + class AccountPrivate < UserError; end end end |