Kiss A simpler, smarter web application framework for Ruby

Rack::EmailErrors Subclass of Object

Methods

Description

Rack::EmailErrors sends error responses (code 5xx) to email addresses as specified in the Rack::Builder config.

Public Class Methods

new(app,agent,app_name,from,*to) source

Not documented

    # File lib/kiss/rack/email_errors.rb, line 5
    def initialize(app,agent,app_name,from,*to)
      @app = app
      @agent = agent == :sendmail ? '/usr/sbin/sendmail -t' : agent
      @app_name = app_name
      @from = from
      @to = to.flatten
    end
    

Public Instance Methods

call(env) source

Not documented

    # File lib/kiss/rack/email_errors.rb, line 13
    def call(env)
      code, headers, body = @app.call(env)
      
      if code >= 500 && code < 600
        begin # rescue any errors in message composition and sending
          error_type = headers['X-Kiss-Error-Type'] || "#{code} Error"
          error_message = headers['X-Kiss-Error-Message']
          
          message = "Content-type: text/html\nFrom: \#{@from}\nTo: \#{@to.join(', ')}\nSubject: \#{@app_name} - \#{error_type}\#{ error_message ? \": \#{error_message}\" : ''}\n\n"
          
          body.each do |part|
            message += part
          end
          
          if @agent.is_a?(String)
            IO.popen(@agent,"w") do |pipe|
              pipe.puts(message)
            end
          else
            require 'net/smtp' unless defined?(Net::SMTP)
            smtp = @agent.is_a?(Net::SMTP) ? @agent : Net::SMTP.new('localhost')
            smtp.start do |smtp|
                        smtp.send_message(message, @from, *@to)
                end
                end
        rescue
        end
        
        body = "<html>\n<head>\n<title>Error</title>\n</head>\n<body>\n<h1>Application Server Error</h1>\n<p>Sorry, an error occurred.  Our technical staff has been notified and will investigate this issue.</p>\n</body>\n</html>\n"
        headers['Content-Length'] = body.length.to_s
      end
      
      [ code, headers, body ]
    end
    

Disabled; run with $DEBUG to generate this.