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

Incorrect status code returned when catching an error using the SimpleRouter::error() function #677

Open
Burial0268 opened this issue Jul 29, 2023 · 3 comments

Comments

@Burial0268
Copy link

Burial0268 commented Jul 29, 2023

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:

use Pecee\Http\Request;

SimpleRouter::error(function (Request $request, \Exception $e) {
    $request->setRewriteCallback(function () use ($e) {
         return (new ErrorManager($e))->handle(); 
    }); 
});

Here return (new ErrorManager($e))->handle(); returns a normal rendered page.

Tried solution: add SimpleRouter::response()->httpCode($e->getCode()); statement

After 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.

@ms-afk
Copy link
Contributor

ms-afk commented Sep 29, 2023

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.
On the other hand, in the second part of your question you are instructing the router to use a different HTTP code in the response, and that is working as well, so no issue here.

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.

@Burial0268
Copy link
Author

Burial0268 commented May 25, 2024

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

@ms-afk
Copy link
Contributor

ms-afk commented May 26, 2024

You have to both set the error and to return the page inside of the function.
Starting from the code you posted in the first post:

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
    }); 
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants