Display a more helpful error message when an ERB template has a Ruby syntax error. #35308
Currently, if code that is intended to be interpolated into a template has a syntax error (e.g.
This PR is an attempt to resolve this issue.
Resolves #34995. (My own issue.)
This is my first time contributing to Rails, so I would certainly appreciate critiques of my coding style and/or my overall approach.
I wasn't able to achieve my ideal user experience; that would be displaying the offending line in context in the "extracted source" box, rather than displaying a blank box, as occurs both before and after my change.
I couldn't see a way to accomplish this without refactors (because
In the end I decided to go with this approach as it seemed like an improvement on the status quo; it might give the user more hints as to what is happening.
Below I've attached screenshots of the user experience in the case where the user has made a Ruby syntax error in an ERB template and visits the page in development mode.
I like the idea of improving this error, but does dumping the entire view source into the message really achieve that? It looks nice for a one-line-view example, but for a more realistic-size view it seems like it's going to be at least as much of an imposing mess as the old one.
@matthewd Thanks for the feedback! I agree this might be an issue, although I'm not yet convinced that in that situation the error message after this change would be worse than the error message was before this change.
What would be a good example case to try this on? Perhaps some multi-line call like:
Ideally, the source code block would contain the view source around the line of interest (does it currently? the screenshot looks like it's empty), but otherwise yeah.
(Though in the reported case, where a paren has been left open, the line of actual interest is the one where that happened, but the line Ruby will consider interesting is the outside-of-template
@erose I'm thinking of an error of that form, inserted into the middle of a view file that looks more like https://github.com/discourse/discourse/blob/master/app/views/layouts/application.html.erb.
You're right -- with my change, this file:
yields an error screen like this
which I think is worse than the situation beforehand.
In that light, I'd like to revert this change. How can that be achieved?
I'd be keen to roll forward, rather than back, if you're interested: to me the substance of your change is "maybe we should actually consider how this error looks", which I'm 10/10 on board for.