Module: TbCore::ErrorHandling

Extended by:
ActiveSupport::Concern
Included in:
ApplicationController
Defined in:
app/controllers/concerns/tb_core/error_handling.rb

Instance Method Summary collapse

Instance Method Details

#class_string(model_name) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'app/controllers/concerns/tb_core/error_handling.rb', line 44

def class_string(model_name)
  string = 'record'
  begin
    object_class = Object.const_get(model_name)
    string = object_class.model_name.human
  rescue NameError # rubocop:disable Lint/HandleExceptions
  end
  string
end

#do_error_response(error) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/concerns/tb_core/error_handling.rb', line 23

def do_error_response(error)
  respond_to do |format|
    format.json { render json: { errors: error.message }, status: error.code }
    format.xml { render xml: { errors: error.message }, status: error.code }
    format.all do
      @error = error
      render template: error.template,
             layout: nil,
             formats: [:html],
             locals: { },
             status: error.code,
             content_type: 'text/html'
    end
  end
end

#handle_record_not_found(exception) ⇒ Object



39
40
41
42
# File 'app/controllers/concerns/tb_core/error_handling.rb', line 39

def handle_record_not_found(exception)
  error = NotFoundError.new(class_string(exception.model))
  handle_request_error(error)
end

#handle_request_error(error) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/concerns/tb_core/error_handling.rb', line 11

def handle_request_error(error)
  error.request_url = request.original_url
  error.template = template_for_request_error() if respond_to?(:template_for_request_error, true)

  if error.is_a?(UnauthorizedError) && request.format.html?
    redirect_to()
    return false
  end

  do_error_response(error)
end

#handle_unknown_format_error(_exception) ⇒ Object



54
55
56
57
# File 'app/controllers/concerns/tb_core/error_handling.rb', line 54

def handle_unknown_format_error(_exception)
  error = NotFoundError.new
  handle_request_error(error)
end