Sha256: e3e2da1262b115fd3dd35979f734cfe3d5f11884e7f46c9bd6a245a65cd9ec91

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

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

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
crystal-0.0.13 lib/crystal/controller/processors/controller_error_handling.rb
crystal-0.0.12 lib/crystal/controller/processors/controller_error_handling.rb
crystal_ext-0.0.11 lib/crystal/controller/processors/controller_error_handling.rb