Skip to content

Handle 401, 403, and 404 errors in frontend #2249

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

Closed
wants to merge 20 commits into from

Conversation

askvortsov1
Copy link
Member

@askvortsov1 askvortsov1 commented Jul 29, 2020

Fixes flarum/issue-archive#206

Changes proposed in this pull request:
Handle 401, 403, and 404 errors in frontend (see commits for specific granular changes)

Reviewers should focus on:

  • See GH review comments

Confirmed

  • Frontend changes: tested on a local Flarum installation.
  • Backend changes: tests are green (run composer test).

Copy link
Contributor

@franzliedke franzliedke left a comment

Choose a reason for hiding this comment

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

I already expressed my doubts about this approach on the issue, but I'd like to add a few general concerns:

  • Again, first and foremost, I can only recommend to avoid complexity like this on error pages - from painful experience.
  • This would render the forum frontend for any 404, even on API and admin, if I am not mistaken.
  • "Model not found" errors must result in 404 status codes, too, to prevent e.g. search engines indexing error pages, and be good internet citizens - if I understand correctly, you would check this in the frontend now, at which point you could not set the status code anymore.

if (! $handled) {
throw $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 to re-implement logic similar to what the registry already does, and the meaning of the additional parameter doesn't seem very intuitive.

@askvortsov1
Copy link
Member Author

#1847 is a MUCH better way to get the actor, without needing to create a second error handler. Thanks Franz!

@stale
Copy link

stale bot commented Dec 31, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. We do this to keep the amount of open issues to a manageable minimum.
In any case, thanks for taking an interest in this software and contributing by opening the issue in the first place!

@stale stale bot added the stale Issues that have had over 90 days of inactivity label Dec 31, 2020
@askvortsov1 askvortsov1 added the org/keep Issues we want to keep open label Jan 5, 2021
@stale stale bot removed the stale Issues that have had over 90 days of inactivity label Jan 5, 2021
@askvortsov1 askvortsov1 force-pushed the as/frontend_error_handler branch 2 times, most recently from 9f25f1e to df99992 Compare March 25, 2021 17:13
@askvortsov1
Copy link
Member Author

  • This would render the forum frontend for any 404, even on API and admin, if I am not mistaken.

No API errors will be handled like this, since it's not on the API middleware stack. And neither will admin 404s, as ALL requests to admin routes are sent to the admin frontend, defaulting to the dashboard page if the route isn't found.

"Model not found" errors must result in 404 status codes, too, to prevent e.g. search engines indexing error pages, and be good internet citizens - if I understand correctly, you would check this in the frontend now, at which point you could not set the status code anymore.

I don't think so? At least from my testing, it returns the page with a 404 response code.

Again, first and foremost, I can only recommend to avoid complexity like this on error pages - from painful experience.

We wouldn't be doing it on proper "error" pages, only on 4xxs, where nothing has gone wrong, it's just that the client's input is in some way invalid.

@askvortsov1 askvortsov1 force-pushed the as/frontend_error_handler branch from b9f8ef5 to 2a79917 Compare March 25, 2021 17:40
@askvortsov1
Copy link
Member Author

Other TODO:

  • Once User::fromRequest method #2703 and Access request actor in error handler #2410 are merged, we should switch to using one ErrorHandler
  • I think we should add a handleInFrontend method to the HandledError class, and provide the list of errors to be handled in the frontend to Registry.
  • We might want to provide both FrontendFormatter and ViewFormatter to HandleErrors? Although that'd be inconsistent with what's needed for API.
  • Alternatively, we could merge FrontendFormatter and ViewFormatter into the same class
  • Design the error page.

@askvortsov1 askvortsov1 force-pushed the as/frontend_error_handler branch from 2b0c4ae to 7cf0df8 Compare April 13, 2021 05:43
@askvortsov1 askvortsov1 force-pushed the as/frontend_error_handler branch from c2e786b to 2c8cc0c Compare April 13, 2021 05:47
@askvortsov1 askvortsov1 force-pushed the as/frontend_error_handler branch from c5d7146 to d7a5bf7 Compare April 13, 2021 05:48
[ci skip] [skip ci]
@@ -57,6 +63,15 @@ public function register()
];
});

$this->container->singleton('flarum.error.contents', function () {
return [
NotAuthenticatedException::class => NotAuthenticated::class,
Copy link
Member Author

Choose a reason for hiding this comment

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

Should these be codes or status strings? That might be cleaner for exceptions added by extensions.

use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class ExecuteErrorToFrontend implements MiddlewareInterface
Copy link
Member Author

Choose a reason for hiding this comment

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

Is this too verbose?

use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class ExecuteErrorToFrontend implements MiddlewareInterface
Copy link
Member Author

Choose a reason for hiding this comment

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

Is this too verbose? ExecuteError?

Copy link
Member

@davwheat davwheat Apr 20, 2021

Choose a reason for hiding this comment

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

Verbosity === self-documentation in my eyes! :)

@@ -65,6 +67,11 @@ public function shouldBeReported(): bool
return $this->type === 'unknown';
}

public function contentClass()
{
return $this->frontendContentClass;
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure I like the naming, especially of the prop


foreach ($middleware as $middlewareClass) {
if (!in_array($middlewareClass, [
'flarum.forum.error_handler',
Copy link
Member Author

Choose a reason for hiding this comment

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

Anything else we want to exclude?


foreach ($middleware as $middlewareClass) {
if (!in_array($middlewareClass, [
'flarum.forum.error_handler',
Copy link
Member Author

Choose a reason for hiding this comment

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

We can't include the error handler to avoid a circular resolution dependency

<div className="ErrorPage">
<div className="container">
<h2>{this.title}</h2>
<p>{app.translator.trans(`core.forum.error.${this.attrs.errorCode}_text`)}</p>
Copy link
Member Author

Choose a reason for hiding this comment

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

@frontend folks, any ideas for making this look... not horrible?

Copy link
Member

Choose a reason for hiding this comment

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

My first idea would be bigger text. 13px is fine for forum posts and dense pages, but an error page needs to be padded out in some form, I think.

We don't want to do too much theming as that should be up to forum maintainers, though.

@askvortsov1 askvortsov1 marked this pull request as ready for review April 13, 2021 05:50
Copy link
Member

@davwheat davwheat left a comment

Choose a reason for hiding this comment

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

If I go to a 404 page, click return to forum, then the back button in my browser, I end up with a broken error page:

image


We might be able to fix this by using history.replaceState() to store the info about the error as part of the browsing history state.

https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState#examples

@davwheat davwheat added this to the 1.1 milestone May 12, 2021
@askvortsov1 askvortsov1 modified the milestones: 1.1, 1.2 Sep 28, 2021
@SychO9 SychO9 removed this from the 1.2 milestone Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
org/keep Issues we want to keep open
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle 404, 403, 410, 401 exceptions in frontend
5 participants