Class: Crystal::Processors::ControllerErrorHandling

Inherits:
Crystal::Processor show all
Defined in:
lib/crystal/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 = crystal.config.send "#{crystal.config.environment!}_error_template", nil
  if tname and Template.exist?(tname, :format => format)            
    data = Template.render(tname, 
      :format => format,               
      :locals => {:error => e}
    )
  else
    e.message
  end
}

Instance Attribute Summary collapse

Attributes inherited from Crystal::Processor

#next_processor

Instance Method Summary collapse

Methods inherited from Crystal::Processor

inspect

Constructor Details

#initialize(next_processor, result_variable = 'content') ⇒ ControllerErrorHandling

Returns a new instance of ControllerErrorHandling.



6
7
8
9
10
# File 'lib/crystal/controller/processors/controller_error_handling.rb', line 6

def initialize next_processor, result_variable = 'content'
  result_variable.must_be.present
  super(next_processor)                
  @result_variable = result_variable
end

Instance Attribute Details

#result_variableObject

Returns the value of attribute result_variable.



4
5
6
# File 'lib/crystal/controller/processors/controller_error_handling.rb', line 4

def result_variable
  @result_variable
end

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/crystal/controller/processors/controller_error_handling.rb', line 12

def call                
  begin
    next_processor.call          
  rescue StandardError => e
    if config.test?
      raise e
    elsif config.production?
      error_shown_to_user = StandardError.new "Internal error!"
      error_shown_to_user.set_backtrace []
    else
      error_shown_to_user = e
    end
      
    format = workspace.params.format
    handler = SPECIAL_ERROR_HANDLERS[format] || DEFAULT_ERROR_HANDLER
    workspace[result_variable] = handler.call error_shown_to_user, format
      
    logger.error e
    logger.info "\n"
  end
end