Class: TemplateStreaming::ErrorRecovery::Middleware

Inherits:
Object
  • Object
show all
Includes:
Rendering
Defined in:
lib/template_streaming/error_recovery.rb

Instance Method Summary collapse

Methods included from Rendering

#uncaught_error_string, #uncaught_errors_html

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



84
85
86
# File 'lib/template_streaming/error_recovery.rb', line 84

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



88
89
90
91
92
93
# File 'lib/template_streaming/error_recovery.rb', line 88

def call(env)
  @env = env
  env[ENV_EXCEPTIONS] = []
  status, headers, @body = *@app.call(env)
  [status, headers, self]
end

#eachObject



95
96
97
98
99
100
101
102
103
104
# File 'lib/template_streaming/error_recovery.rb', line 95

def each
  # Assume there are no faux occurrences of </body>.
  @body.each do |chunk|
    if render_errors? && (chunk =~ %r'</body\b')
      errors = @env[ENV_EXCEPTIONS]
      chunk.insert($~.begin(0), uncaught_errors_html(errors))
    end
    yield chunk
  end
end