Sha256: 3cc02567909a44380fb9951c7630153e6f7b1b69a4abfe066a9fba6e6ac3c74c

Contents?: true

Size: 1.28 KB

Versions: 12

Compression:

Stored size: 1.28 KB

Contents

rad.controller

class ControllerErrorHandling < Rad::Conveyors::Processor      
  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          

  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
  }
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rad_core-0.2.6 lib/rad/controller/processors/controller_error_handling.rb
rad_core-0.2.5 lib/rad/controller/processors/controller_error_handling.rb
rad_core-0.2.4 lib/rad/controller/processors/controller_error_handling.rb
rad_core-0.2.3 lib/rad/controller/processors/controller_error_handling.rb
rad_core-0.2.2 lib/rad/controller/processors/controller_error_handling.rb
rad_core-0.2.1 lib/rad/controller/processors/controller_error_handling.rb
rad_core-0.2.0 lib/rad/controller/processors/controller_error_handling.rb
rad_core-0.0.30 lib/rad/controller/processors/controller_error_handling.rb
rad_core-0.0.29 lib/rad/controller/processors/controller_error_handling.rb
rad_core-0.0.28 lib/rad/controller/processors/controller_error_handling.rb
rad_core-0.0.27 lib/rad/controller/processors/controller_error_handling.rb
rad_core-0.0.26 lib/rad/controller/processors/controller_error_handling.rb