-
Notifications
You must be signed in to change notification settings - Fork 129
Improve display of 404 pages #572
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
Conversation
@tf this is just an idea I had that will deal with this error gracefully, in my opinion. By raising this error we can let other layers deal with this problem. In this case that will be the application controller, but I would actually suggest to not rescue that and let the host application deal with it. For example, I know that the regular 404 had hard-coded German in it, which is not something easily useful for English apps. I would say that raising is the most appropriate thing to do, even if we're not strictly trying to find a non-existing ActiveRecord. Your ideas? When pursued the I never could find a reference to the |
You make some compelling points and, as #40 states, things are currently a bit broken in this area. First, I'd suggest removing the Regarding removing all rescuing logic I'm skeptical. Even if Keeping files in For more complex integration, I'd otherwise suggest doing this via new config options, i.e. allowing to register a lambda that handles the case where an entry is not found. That could then deal with specific needs like displaying a localized error page etc. Thoughts? |
Tweaking the generator is an excellent idea! This is the least surprising alternative and quite flexible too. I might be able to start work on this next week. |
it "copies the images" do | ||
run_generator | ||
|
||
expect(file("public/pageflow/images/bg.jpg")).to exist |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer single-quoted strings when you don't need string interpolation or special symbols.
@tf I pushed some new commits. Once people upgrade to 0.11/0.12 we would have to urge them to re-run the generator. |
invoke 'pageflow:user' | ||
invoke 'pageflow:seeds' | ||
invoke 'pageflow:active_admin_initializer' | ||
invoke 'pageflow:not_found' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think pageflow:error_pages
would be a bit more descriptive and would also allow to include files for other status codes should we ever need them.
Looks really good already! And for the upgrade step running the |
format.any(:json, :css) { head :not_found } | ||
format.html do | ||
begin | ||
render file: Rails.public_path.join('pageflow', 'error_pages', '404.html'), status: :not_found |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long. [106/100]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still unsure if we should have a test for this? The line itself is covered. We could add an expection to the EntriesController#show
spec that some text from the generated file shows up. For the rescue clause below, I see no way to get test coverage. A test could temporarily rename the 404.html
file in the dummy app. But I'm not sure it's worth it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's worth it because the head :not_found
in ApplicationController is part of our code. I added 527f703 to test this. It might not look pretty but it is effective. I added the safeguard since the test could fail for other reasons and leave the dummy app in an unclean state.
An alternative could be to not bother with file-level stuff (which technically doesn't belong in a controller test anyway) but stub out the call to render
and raise ActionView::MissingTemplate
from the test. But then you're testing Rails framework detail which might change at some point...
Not sure what we should prefer here. But I do think a test has value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as testing response content, that has little additional value to me over the status code. Also it makes the test brittle in case someone wants to put in new text in this file.
Yes. Just pushed up the new commit to move error pages into its own directory. Final review? |
<html> | ||
<head> | ||
<meta charset="utf-8"/> | ||
<title>Beitrag nicht gefunden (404)</title> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generated default file should probably be in english as that's also the default locale.
Added two more notes. Not sure about the test issue. Please again squash to one descriptive commit when you're done. |
|
||
def without_error_pages | ||
error_pages = __dir__ + "/../../dummy/rails-#{Pageflow::RailsVersion.detect}/public/pageflow/error_pages" | ||
fail "Pageflow error pages not present in the dummy app. Remove `spec/dummy` to regenerate it." unless File.exists?("#{error_pages}/404.html") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long. [148/100]
Prefer single-quoted strings when you don't need string interpolation or special symbols.
The test does not seem to work. I got suspicious when I saw that the Regarding testing the response body: The current test does not really ensure the template is used. For all we know this could also be the Finally, I'm not so fond of using |
Introduce a generator that generates the static 404.html into the host application's public folder. Pageflow will try to render this page in case of a PageNotFound. When the page is absent, a :not_found 'head' response is given instead. Upgrading host applications should run the pageflow:error_pages generator to generate these files. Afterwards they can be customized at will. This change also takes out the format.any fallbacks, since these have been handled in the ApplicationController for a while anyway. Fixes #40
@tf I took out the test and left it at that for the moment. Adding the config feels a little scope-creepy to me, but not a bad idea in principle. If we're doing that only to satisfy the test case, that's probably not the right reason though. I will be on vacation for three weeks, with little access to GitHub. |
Agreed. Have a nice vacation. |
With this change, the EntriesController will now raise
ActiveRecord::RecordNotFound
for all unknown formats.This bubbles up to the ApplicationController where it is currently
dealth with in a
rescue_from
block. This has been changed to simplyrender a 404
head
response.This frees us from trying to come up with appropriate response formats
while still giving the correct HTTP header.
I would strongly prefer to entirely ditch the rescuing from
ActiveRecord::RecordNotFound and let the host application deal with this
problem. It seems something that is very specific to the host, and not
up to Pageflow.
fixes #40