Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream Rails error pages #173

Merged
merged 4 commits into from
Mar 22, 2024
Merged

Stream Rails error pages #173

merged 4 commits into from
Mar 22, 2024

Conversation

joeldrapper
Copy link
Collaborator

No description provided.

Co-Authored-By: Mikael Henriksson <[email protected]>
@@ -17,10 +17,29 @@ def stream(view)
self.response_body = Enumerator.new do |buffer|
view.call(buffer, view_context: view_context)
rescue => e
buffer << %('">)
buffer << view_context.javascript_tag(nonce: true) { %(window.location = "/500.html").html_safe }
raise (e) if Rails.env.test?
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In test environments, I think we want to just raise.

Comment on lines 22 to 27
debug_middleware = ActionDispatch::DebugExceptions.new(
proc { |env| raise(e) },
response_format: :html
)

_debug_status, _debug_headers, debug_body = debug_middleware.call(request.env)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We can call the ActionDispatch::DebugExceptions middleware to get the debug page HTML, which we can send via JavaScript to the streaming client.


if Rails.env.development?
js = <<~JAVASCRIPT
document.documentElement.innerHTML = "#{view_context.j(debug_body.join)}";
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In development, we send some JS that replaces the document with the debug body, so you get nice error pages.

buffer << %(-->"'>)
buffer << view_context.javascript_tag(js, nonce: true)

p e
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems left over from local testing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, it’s so you can see the error in the console. We can probably improve this somewhat, but we should have some useful output in the console as well as the page.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We can’t use raise it again, because it blocks the request. Unless there’s some way to manually tell ActionDispatch to close the request.


// Re-evaluate all script tags
document.querySelectorAll("script").forEach((script) => {
newScript = document.createElement("script");
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks awfully familiar!!

@joeldrapper joeldrapper marked this pull request as ready for review March 22, 2024 22:37
@joeldrapper joeldrapper merged commit b7f7038 into main Mar 22, 2024
13 checks passed
@joeldrapper joeldrapper deleted the streaming-errors branch March 22, 2024 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants