module Crystal module Processors class ControllerErrorHandling < Processor attr_accessor :result_variable def initialize next_processor, result_variable = 'content' result_variable.must_be.present super(next_processor) @result_variable = result_variable end 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 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 } end end end