Sha256: 4283e127dd473aba92c04d2211e303e29f4bae09884fb3edced14d80e5396fea

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

module Rad
  module Processors    
    class ControllerErrorHandling < Processor      
      def call                
        workspace.response.must_be.defined
        
        begin
          next_processor.call          
        rescue StandardError => e
          if config.test?
            e.set_backtrace e.backtrace.sfilter(Exception.common_filters)
            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
          
          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          
    
      SPECIAL_ERROR_HANDLERS = {
        'json' => lambda{|e, format| 
          {error: e.message}.to_json
        }
      }
      
      DEFAULT_ERROR_HANDLER = lambda{|e, format|
        tname = rad.config["#{rad.config.environment!}_error_template", nil]
        
        if tname and Template.exist?(tname, format: format, exact_format: true)
          data = Template.render(tname, 
            format: format,               
            locals: {error: e}
          )
        else
          e.message
        end
      }
    end    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rad_core-0.0.13 lib/rad/controller/processors/controller_error_handling.rb