Class: ControllerErrorHandling

Inherits:
Rad::Conveyors::Processor show all
Defined in:
lib/rad/controller/processors/controller_error_handling.rb

Constant Summary collapse

SPECIAL_ERROR_HANDLERS =
{
  'json' => lambda{|e, format|
    {error: e.message}.to_json
  }
}
DEFAULT_ERROR_HANDLER =
lambda{|e, format|
  tname = rad.controller.send("#{rad.mode}_error_template")
  if tname and rad.template.exist?(tname, format: format, exact_format: true)
    data = rad.template.render(tname,
      format: format,
      locals: {error: e}
    )
  else
    e.message
  end
}

Instance Attribute Summary

Attributes inherited from Rad::Conveyors::Processor

#next_processor

Instance Method Summary collapse

Methods inherited from Rad::Conveyors::Processor

#initialize, inspect

Constructor Details

This class inherits a constructor from Rad::Conveyors::Processor

Instance Method Details

#callObject



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
# File 'lib/rad/controller/processors/controller_error_handling.rb', line 4

def call
  workspace.response.must_be.defined

  begin
    next_processor.call
  rescue StandardError => e
    if rad.test?
      # e.set_backtrace e.backtrace.sfilter(Exception.filters)
      raise e
    elsif rad.production?
      error_shown_to_user = StandardError.new "Internal error!"
      error_shown_to_user.set_backtrace []
    else
      error_shown_to_user = e
    end

    workspace.response.clear if workspace.response
    format = workspace.params.format
    handler = SPECIAL_ERROR_HANDLERS[format] || DEFAULT_ERROR_HANDLER
    workspace.content = handler.call error_shown_to_user, format


    logger.error e
    logger.info "\n"
  end
end