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

[Core: Module] Handle 500 errors properly #4163

Merged
merged 3 commits into from
Dec 10, 2018
Merged

[Core: Module] Handle 500 errors properly #4163

merged 3 commits into from
Dec 10, 2018

Conversation

johnsaigle
Copy link
Contributor

@johnsaigle johnsaigle commented Nov 29, 2018

Brief summary of changes

Previously LORIS would crash and show a blank page on 500 errors OR mistakenly show a 404 page.

This was because the try-catch in Module only had a catch case for NotFound exceptions. Thus all exceptions were treated as 404 as a result even though it was wrong of reasons.

On top of this, the actual exception message would be swallowed and would not appear in the error logs, making things very difficult to debug.

This PR fixes this issues and serves well-decorated 500 pages containing a message to the user. A special message is returned for ConfigurationExceptions as they reflect errors on the user's part rather than on LORIS's part. The exception information is also logged to assist with debugging.

This resolves issue...

Should address numerous issues that have arisen in PRs #3921 #3881 #4162, maybe more.

To test this change...

  • Throw an exception in the code anywhere. Instead of crashing you will either get a well-decorated generic 500 page or a page telling you that you have invalid Config settings.

@johnsaigle johnsaigle added Category: Bug PR or issue that aims to report or fix a bug Category: Feature PR or issue that aims to introduce a new feature [branch] major labels Nov 29, 2018
@johnsaigle johnsaigle changed the base branch from major to bugfix November 29, 2018 19:44
@driusan
Copy link
Collaborator

driusan commented Nov 29, 2018

That explanation doesn't make sense.. if it doesn't have catches for other exception types they keep propagating, they don't get handled by the wrong handler.

try {
    throw new Exception("Test");
} catch (BadFunctionCallException $e) { /* A random more specific exception type from SPL */
    print "I caught something";
}

print "I am here";

Results in:

$ php test.php 
PHP Fatal error:  Uncaught Exception: Test in /Users/driusan/Code/Loris/test.php:4
Stack trace:
#0 {main}
  thrown in /Users/driusan/Code/Loris/test.php on line 4

Fatal error: Uncaught Exception: Test in /Users/driusan/Code/Loris/test.php:4
Stack trace:
#0 {main}
  thrown in /Users/driusan/Code/Loris/test.php on line 4

(But it's still an improvement to handle more types, so there's no reason to block it just because the explanation doesn't make sense..)

@@ -265,6 +271,7 @@ class Module extends \LORIS\Router\PrefixRouter implements RequestHandlerInterfa
);
}
} catch (\NotFound $e) {
error_log("IN the NotFound exception");
Copy link
Collaborator

Choose a reason for hiding this comment

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

debug statement

);
} catch (\ConfigurationException $e) {
error_log($e);
error_log(self::CONFIGURATION_ERROR);
Copy link
Collaborator

@driusan driusan Nov 29, 2018

Choose a reason for hiding this comment

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

there's no reason to put the message for the users in the error log, you've already logged the exception.

@@ -1,6 +1,4 @@
<div class="container">
{foreach from=$error_message item=message}
<h2>{$message}</h2>
{/foreach}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This shouldn't be changed from an array to a scalar, that will break any other places that use it.

Copy link
Contributor Author

@johnsaigle johnsaigle Nov 29, 2018

Choose a reason for hiding this comment

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

The issue wasn't so much of array/scalar but that it's inconsistent with

$tpl_data = array(
(which uses message instead of error_message).

Do you prefer to change the Error class to use error_message or should the other calling code across LORIS be changed?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't actually know how many calling places there are, but wouldn't it be easier to change that to 'message' => [$message] instead of trying to hunt down the other ones?

Copy link
Contributor Author

@johnsaigle johnsaigle Nov 29, 2018

Choose a reason for hiding this comment

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

If you mean change the Error.php code, then I would need to change the 403 and 404 templates as well as their calling code instead; they use scalars instead of arrays in their templates (in contrast to 500).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have a preference but they need to be standardized one way or another if they are going to use src/Http/Error.php.

@@ -277,8 +284,75 @@ class Module extends \LORIS\Router\PrefixRouter implements RequestHandlerInterfa
)
)
);
/* Catch more specific errors that may be thrown in the codebase.
* Without handling these exceptions explicitly, LORIS will display
* a 404 error and swallow the exception, hindering debugging efforts.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't like this first paragraph because that's not actually how PHP handles exceptions.

(The second paragraph is fine but there's no reason to have the word "NOTE" at the front.. it's just a comment.)

@johnsaigle johnsaigle added the State: Discussion required PR or issue that requires the resolution of a discussion with the relevant parties to proceed label Nov 29, 2018
@johnsaigle
Copy link
Contributor Author

@ridz1208 This is the PR we were talking about

@ridz1208
Copy link
Collaborator

ridz1208 commented Dec 5, 2018

@driusan please re-review

@davidblader davidblader removed Release: Add to release notes PR whose changes should be highlighted in the release notes labels Dec 7, 2018
@driusan driusan merged commit 5fc333c into aces:bugfix Dec 10, 2018
@ridz1208 ridz1208 added this to the 20.1.2 milestone Dec 14, 2018
@johnsaigle johnsaigle deleted the 181129-Handle500Gracefully branch January 23, 2019 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Bug PR or issue that aims to report or fix a bug Category: Feature PR or issue that aims to introduce a new feature State: Discussion required PR or issue that requires the resolution of a discussion with the relevant parties to proceed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants