Sha256: 1d1e7f22c33e2cf5cce7572f5f922b0ff641c6f0012de700b73bf2c9a8c71942

Contents?: true

Size: 1.95 KB

Versions: 14

Compression:

Stored size: 1.95 KB

Contents

# DEPRECATED
# This module is now deprecated as slated to be removed in Kiss 1.6.

module Rack
  # Rack::EmailErrors sends error responses (code 5xx) to email addresses as
  # specified in the Rack::Builder config.
  class EmailErrors
    def initialize(app, *args)
      @_app = app
      
      if (options = args.first).is_a?(Hash)
        @_agent = options[:agent]
        @_subject = options[:subject]
        @_from = options[:from]
        @_to = options[:to]
      else
        @_agent, @_subject, @_from, *@_to = *args
      end
      
      @_agent = '/usr/sbin/sendmail -t' if @_agent == :sendmail
    end

    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 = <<-EOT
Content-type: text/html
From: #{@_from}
To: #{@_to.join(', ')}
Subject: #{@_subject} - #{error_type}#{ error_message ? ": #{error_message}" : ''}

EOT
          
          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 = <<-EOT
<html>
<head>
<title>Error</title>
</head>
<body>
<h1>Application Server Error</h1>
<p>Sorry, an error occurred.  Our technical staff has been notified and will investigate this issue.</p>
</body>
</html>
EOT
        headers['Content-Length'] = body.content_length.to_s
      end
      
      [ code, headers, body ]
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
kiss-1.8.9 lib/kiss/rack/email_errors.rb
kiss-1.8.8 lib/kiss/rack/email_errors.rb
kiss-1.8.7 lib/kiss/rack/email_errors.rb
kiss-1.8.6 lib/kiss/rack/email_errors.rb
kiss-1.8.5 lib/kiss/rack/email_errors.rb
kiss-1.8.4 lib/kiss/rack/email_errors.rb
kiss-1.8.3 lib/kiss/rack/email_errors.rb
kiss-1.8.2 lib/kiss/rack/email_errors.rb
kiss-1.8.1 lib/kiss/rack/email_errors.rb
kiss-1.8 lib/kiss/rack/email_errors.rb
kiss-1.7.4 lib/kiss/rack/email_errors.rb
kiss-1.7.2 lib/kiss/rack/email_errors.rb
kiss-1.7.1 lib/kiss/rack/email_errors.rb
kiss-1.7 lib/kiss/rack/email_errors.rb