aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/errors_controller.rb
blob: 112e2d37e73d6e7796a71a7380ce52771cb005fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class ErrorsController < ApplicationController
  before_action :force_format
  layout :select_layout

  def render_error
    @exception = env["action_dispatch.exception"]

    case @exception
    when OAuth::Unauthorized
      # /i/callback
      redirect_to root_path
    when Aclog::Exceptions::LoginRequired
      @status = 403
      @message = t("error.login_required")
    when Aclog::Exceptions::OAuthEchoUnauthorized
      @status = 401
      @message = t("error.oauth_echo_unauthorized")
    when ActionController::RoutingError,
         ActiveRecord::RecordNotFound,
         ActionView::MissingTemplate
      @status = 404
      @message = t("error.routing_error")
    when Aclog::Exceptions::UserNotRegistered
      @status = 404
      @message = t("error.user_not_registered")
    when Aclog::Exceptions::UserProtected
      @status = 403
      @message = t("error.user_protected")
    when Aclog::Exceptions::AccountPrivate
      @status = 403
      @message = t("error.account_private")
    else
      @status = 500
      @message = "#{t("error.internal_error")}: #{@exception.class}"
    end

    if @exception.is_a? Aclog::Exceptions::UserError
      @user = @exception.user
    end

    render status: @status
  end

  private
  def select_layout
    @user ? nil : "index"
  end

  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