Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/react/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
module React
class Renderer

class PrerenderError < RuntimeError
def initialize(component_name, props, js_message)
message = "Encountered error \"#{js_message}\" when prerendering #{component_name} with #{props.to_json}"
super(message)
end
end

cattr_accessor :pool

def self.setup!(react_js, components_js, args={})
Expand Down Expand Up @@ -56,13 +63,8 @@ def render(component, args={})
}()
JS
context.eval(jscode).html_safe
# What should be done here? If we are server rendering, and encounter an error in the JS code,
# then log it and continue, which will just render the react ujs tag, and when the browser tries
# to render the component it will most likely encounter the same error and throw to the browser
# console for a better debugging experience.
rescue ExecJS::ProgramError => e
::Rails.logger.error "[React::Renderer] #{e.message}"
raise PrerenderError.new(component, args, e)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we log the stack trace too?

end

end
end
8 changes: 8 additions & 0 deletions test/react_renderer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ class ReactRendererTest < ActiveSupport::TestCase
end
end
end

test 'prerender errors are thrown' do
err = assert_raises React::Renderer::PrerenderError do
React::Renderer.render("NonexistentComponent", {error: true, exists: false})
end
expected_message = 'Encountered error "ReferenceError: NonexistentComponent is not defined" when prerendering NonexistentComponent with {"error":true,"exists":false}'
assert_equal expected_message, err.message
end
end