I have set up renderer in the default error handler to display custom error pages which works fine; when I for instance type in a non-existent url path it will render my custom error page with the correct 404 status code.
But how can I display custom error page when manually triggering an http error, say error 401 with $response->withStatus(401)? Anything I write to the response body (or header) or template I try to render when triggering an http error is ignored and the default Apache error for that status code is displayed instead.
Hi, yes I have tried that, too. Same issue. The status code is correct but it only displays the Apache default error. I’ve tried with HttpNotFoundException.
Sorry to nag, but can you please explain to me why this happens? I am using the default error handler to test now. The json 404 error is shown if I type a bogus url, but when I throw the error manually, or use $response->withStatus(404), the default Apache error is shown, no matter what template I render or text I write to the body.
I use the exact same code as you have there, from inside an Action that does nothing else. I am not getting a 500 server error, I get the correct status, ie 404 (or whatever error I throw), but I cannot use custom body for the error.
It is set to the default App\Handler\DefaultErrorHandler. Even that without ANY modification has the same problem. The error details are not shown when manually triggering the error.
Yes, it is set and like I said, it works (shows the details) if I try to open a non-existant path in the url. It only does not work when I trigger the error manually. I don’t know how else I can explain it.
But when I trigger the error manually in the code, by using throw new HttpNotFoundError($request);, the correct status is shown but not the error details. The default Apache error is shown.
In other words, when I trigger the error manually using throw new HttpNotFoundError($request); I get this:
mod_rewrite is reported as not found by the checker, but it is enabled and works. The methods used in that checker will not detect mod_rewrite when running php-fpm.
I am stumped. It cannot be a mod_rewrite problem anyway, since it works when the error is thrown from the routing middleware. Am I right? It would seem to me that throwing it from the Action, the body and headers are replaced somewhere.
But anyway, rewriting works and this is my .htaccess file:
# Redirect to front controller
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]