Class: TemplateStreaming::ErrorRecovery::Middleware::BodyProxy

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

Instance Method Summary collapse

Constructor Details

#initialize(env, body) ⇒ BodyProxy

Returns a new instance of BodyProxy.



22
23
24
25
26
# File 'lib/template_streaming/error_recovery.rb', line 22

def initialize(env, body)
  @env = env
  @body = body
  @controller = @env[CONTROLLER_KEY]
end

Instance Method Details

#advance_state(chunk, cursor = 0) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/template_streaming/error_recovery.rb', line 47

def advance_state(chunk, cursor=0)
  case @state
  when :start
    if index = chunk.index(%r'<!doctype\b.*?>'i, cursor)
      @state = :before_html
      advance_state(chunk, index)
    end
  when :before_html
    if index = chunk.index(%r'<html\b'i, cursor)
      @state = :before_head
      advance_state(chunk, index)
    end
  when :before_head
    if index = chunk.index(%r'<head\b'i, cursor)
      @state = :in_head
      advance_state(chunk, index)
    end
  when :in_head
    if index = chunk.index(%r'</head\b.*?>'i, cursor)
      @state = :between_head_and_body
      advance_state(chunk, index)
    end
  when :between_head_and_body
    if index = chunk.index(%r'<body\b'i, cursor)
      @state = :in_body
      advance_state(chunk, index)
    end
  when :in_body
    if index = chunk.index(%r'</body\b.*?>'i, cursor)
      @state = :after_body
      advance_state(chunk, index)
    end
  when :after_body
    if index = chunk.index(%r'</html\b.*?>'i, cursor)
      @state = :after_html
      advance_state(chunk, index)
    end
  end
end

#each(&block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/template_streaming/error_recovery.rb', line 28

def each(&block)
  if @controller && @controller.show_errors?
    exceptions = @env[EXCEPTIONS_KEY] = []
    @state = :start
    @body.each do |chunk|
      advance_state(chunk)
      if !exceptions.empty?
        try_to_insert_errors(chunk, exceptions)
      end
      yield chunk
    end
    if !exceptions.empty?
      yield uninserted_errors(exceptions)
    end
  else
    @body.each(&block)
  end
end

#render_exceptions(exceptions) ⇒ Object



126
127
128
129
# File 'lib/template_streaming/error_recovery.rb', line 126

def render_exceptions(exceptions)
  template = @controller.response.template
  template.render_streaming_exceptions(exceptions)
end

#try_to_insert_errors(chunk, exceptions) ⇒ Object



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

def try_to_insert_errors(chunk, exceptions)
  if (index = chunk =~ %r'</body\s*>\s*(?:</html\s*>\s*)?\z'im)
    chunk.insert(index, render_exceptions(exceptions))
    exceptions.clear
  end
end

#uninserted_errors(exceptions) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/template_streaming/error_recovery.rb', line 94

def uninserted_errors(exceptions)
  html = render_exceptions(exceptions)
  exceptions.clear
  case @state
  when :start
    head = "<head><title>Unhandled Exception</title></head>"
    body = "<body>#{html}</body>"
    "<!DOCTYPE html><html>#{head}#{body}</html>"
  when :before_html
    head = "<head><title>Unhandled Exception</title></head>"
    body = "<body>#{html}</body>"
    "<html>#{head}#{body}</html>"
  when :before_head
    head = "<head><title>Unhandled Exception</title></head>"
    body = "<body>#{html}</body>"
    "#{head}#{body}</html>"
  when :in_head
    "</head><body>#{html}</body></html>"
  when :between_head_and_body
    "<body>#{html}</body></html>"
  when :in_body
    "#{html}</body></html>"
  when :after_body
    # Errors aren't likely to happen at this point, as after the body
    # there should only be "</html>". Just stick our error html in there
    # - it's invalid HTML no matter what we do.
    "#{html}</html>"
  when :after_html
    html
  end
end