# Manually trigger HTTP ERROR with response body

**URL:** <https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953>\
**Category:** Questions\
**Created:** [September 21, 2021, 2:27pm UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953 "2021-09-21T14:27:00Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![blegh](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@blegh](https://discourse.slimframework.com/u/blegh)\
**Post date:** [September 21, 2021, 2:27pm UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/1 "2021-09-21T14:27:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [September 21, 2021, 5:53pm UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/2 "2021-09-21T17:53:38Z")

</div>

The simplest solution is to use one of [Slim’s HttpException classes](https://www.slimframework.com/docs/v4/middleware/error-handling.html#new-http-exceptions):

```php
use Slim\Exception\HttpUnauthorizedException;
// ...

throw new HttpUnauthorizedException($request);

```

---

<div class="post-metadata">

**Author:** ![blegh](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@blegh](https://discourse.slimframework.com/u/blegh)\
**Post date:** [September 21, 2021, 6:07pm UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/3 "2021-09-21T18:07:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![blegh](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@blegh](https://discourse.slimframework.com/u/blegh)\
**Post date:** [September 22, 2021, 8:41am UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/4 "2021-09-22T08:41:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [September 22, 2021, 10:50am UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/5 "2021-09-22T10:50:15Z")

</div>

Have you added the Slim ErrorMiddleware?

---

<div class="post-metadata">

**Author:** ![blegh](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@blegh](https://discourse.slimframework.com/u/blegh)\
**Post date:** [September 22, 2021, 11:21am UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/6 "2021-09-22T11:21:53Z")

</div>

I have the following as default per the Slim 4 Skeleton:

```auto
return function (App $app) {
    $app->addBodyParsingMiddleware();
    $app->add(ValidationExceptionMiddleware::class);
    $app->addRoutingMiddleware();
    $app->add(BasePathMiddleware::class);
    $app->add(ErrorMiddleware::class);
};

```

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [September 22, 2021, 12:30pm UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/7 "2021-09-22T12:30:35Z")

</div>

Can you show the code how you throw the exception?

Make sure you pass the request object to the constructor. Example:

```php
use Slim\Exception\HttpNotFoundException;
// ...
throw new HttpNotFoundException($request);

```

Otherwise you will get just a Internal Server Error 500 `Too few arguments to function lim\\Exception\\HttpSpecializedException::__construct()`

---

<div class="post-metadata">

**Author:** ![blegh](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@blegh](https://discourse.slimframework.com/u/blegh)\
**Post date:** [September 22, 2021, 12:36pm UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/8 "2021-09-22T12:36:29Z")

</div>

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.

EDIT: The request object is passed to \_\_invoke().

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [September 22, 2021, 12:53pm UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/9 "2021-09-22T12:53:49Z")

</div>

In this case, make sure you add a default error handler to the Slim ErrorMiddleware within the DI container definiton for `ErrorMiddleware::class`

```php
$errorMiddleware->setDefaultErrorHandler($customErrorHandler);

```

- [Adding Custom Error Handlers](http://dev.slimframework.com/docs/v4/middleware/error-handling.html#adding-custom-error-handlers)
- [Full example](https://github.com/odan/slim4-skeleton/blob/master/config/container.php#L102-L122)

---

<div class="post-metadata">

**Author:** ![blegh](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@blegh](https://discourse.slimframework.com/u/blegh)\
**Post date:** [September 22, 2021, 1:03pm UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/10 "2021-09-22T13:03:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [September 22, 2021, 1:13pm UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/11 "2021-09-22T13:13:21Z")

</div>

> [@blegh](#):
>
> The error details are not shown when manually triggering the error.

Can you post a screenshot?

BTW: The error details are disabled by default (for security reasons).  
You can change it in the settings:

```php
$settings['error']['display_error_details'] = true;

```

---

<div class="post-metadata">

**Author:** ![blegh](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@blegh](https://discourse.slimframework.com/u/blegh)\
**Post date:** [September 22, 2021, 1:35pm UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/12 "2021-09-22T13:35:35Z")

</div>

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.

If I use address [http://myserver/some/path/that/does/notexist](http://myserver/some/path/that/does/notexist), or when there is a 500 server error, then everything works fine (the json error details are shown).

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:

 ![default_apache_error](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/2/26914566a0688b0b8957e9a2e3d50035a5a9d166.png)

…instead of the error details.

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [September 22, 2021, 4:32pm UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/13 "2021-09-22T16:32:36Z")

</div>

OK, this is very strange. Normally this is not the default behavior. I guess that Apache could be configured in a special way.

---

<div class="post-metadata">

**Author:** ![blegh](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@blegh](https://discourse.slimframework.com/u/blegh)\
**Post date:** [September 22, 2021, 5:23pm UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/14 "2021-09-22T17:23:16Z")

</div>

I’ve been searching through Apache documentation, but no luck.

But why does it work when the framework throws the error and not when I do it manually from an action?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [September 23, 2021, 8:35am UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/15 "2021-09-23T08:35:42Z")

</div>

Technically it’s the same, because the Slim RoutingMiddleware also throws the `Slim\Exception\HttpNotFoundException` in line 91. See [here](https://github.com/slimphp/Slim/blob/4.x/Slim/Middleware/RoutingMiddleware.php#L91).

Try to check your mod\_rewrite rules in your .htaccess files.  
You could also try this [Slim setup checker](https://github.com/odan/slim-checker) and post the result.

---

<div class="post-metadata">

**Author:** ![blegh](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@blegh](https://discourse.slimframework.com/u/blegh)\
**Post date:** [September 23, 2021, 12:46pm UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/16 "2021-09-23T12:46:27Z")

</div>

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:

```auto
# Redirect to front controller
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

```

---

<div class="post-metadata">

**Author:** ![blegh](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@blegh](https://discourse.slimframework.com/u/blegh)\
**Post date:** [September 23, 2021, 1:52pm UTC](https://discourse.slimframework.com/t/manually-trigger-http-error-with-response-body/4953/17 "2021-09-23T13:52:00Z")

</div>

OK, got it working. I realised it was only an issue when using the ‘/’ root path. So I changed the .htaccess to:

```auto
RewriteEngine On
RewriteBase /
RewriteRule ^ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]

```

Thanks for all your help.
