-
Notifications
You must be signed in to change notification settings - Fork 122
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
Incorrect status code returned when catching an error using the SimpleRouter::error() function #677
Comments
In the example above you get a 200 HTTP error because, inside of your error handler, you are just rendering a page through the ErrorManager object and you are never telling php or the router to send a custom HTTP code in the response's header (or at least this is what I think you are [not] doing, given that I don't have the code of ErrorManager). So the default HTTP code sent by php/SimpleRouter will be the 200 code. The problem you are talking about in the end is caused by your nginx configuration, not by this library. Try looking here, it seems to be a similar issue to yours: https://stackoverflow.com/questions/59495015/index-html-should-return-404-but-shows-nginx-welcome-page-instead. |
use Pecee\Http\Request;
SimpleRouter::error(function(Request $request, \Exception $exception) {
response()->httpCode($exception->getCode());
});
return (new ErrorManager($exception))->handle(); If follow your view, the code would theoretically work, but it would only end up returning the status code and the page would not be rendered correctly |
You have to both set the error and to return the page inside of the function. use Pecee\SimpleRouter\SimpleRouter;
use Pecee\Http\Request;
SimpleRouter::error(function (Request $request, \Exception $e) {
$request->setRewriteCallback(function () use ($e) {
SimpleRouter::response()->httpCode(404); //set the http code first
return (new ErrorManager($e))->handle(); //return the page second
});
}); |
As you can see, when I use SimpleRouter::error() in conjunction with Request's setRewriteCallback() method to catch an error route, instead of getting an HTTP status code of 4XX or 5XX, I get 200.
Here is my code:
Here
return (new ErrorManager($e))->handle();
returns a normal rendered page.Tried solution: add
SimpleRouter::response()->httpCode($e->getCode());
statementAfter adding the
SimpleRouter::response()->httpCode($e->getCode());
statement, the status code is correct, but the default Nginx interface is displayed instead of the error page that I set.It seems that all sites that use this Router(include official demo) have this problem.
If you need a more detailed code, reply here.
The text was updated successfully, but these errors were encountered: