# Not Found Handler without Error Middleware?

**URL:** <https://discourse.slimframework.com/t/not-found-handler-without-error-middleware/5317>\
**Category:** Questions\
**Created:** [July 25, 2022, 3:03pm UTC](https://discourse.slimframework.com/t/not-found-handler-without-error-middleware/5317 "2022-07-25T15:03:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbcl](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@dbcl](https://discourse.slimframework.com/u/dbcl)\
**Post date:** [July 25, 2022, 3:03pm UTC](https://discourse.slimframework.com/t/not-found-handler-without-error-middleware/5317/1 "2022-07-25T15:03:58Z")

</div>

In Slim3 I was able to disable the default Error Handler but still use the default Not Found Handler (404). I think this is not possible in Slim4, as the Error Middleware must be added to Slim in order to set the Not Found Handler.

As I would still like to use my custom error handler with Slim 4, how would you recommend I go about handling 404s? I think some options are:

- use .htaccess in Apache
- create a Slim catch all else route which returns custom 404 response
- somehow register my custom error handler as the Slim Error Middleware

---

<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:** [July 26, 2022, 7:03am UTC](https://discourse.slimframework.com/t/not-found-handler-without-error-middleware/5317/2 "2022-07-26T07:03:55Z")

</div>

> [@dbcl](#):
>
> Error Middleware must be added to Slim in order to set the Not Found Handler.

In Slim 4 the ErrorMiddleware is still optional to add a custom 404 Not found handler.  
You can add a custom NotFoundErrorMiddleware with a try/catch block, that catches the Slim `HttpNotFoundException` in the catch block and let the ErrorMiddleware handle all other uncatched errors.  
It is also possible to remove the ErrorMiddleware completely, and handle everything yourself.

---

<div class="post-metadata">

**Author:** ![dbcl](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@dbcl](https://discourse.slimframework.com/u/dbcl)\
**Post date:** [July 31, 2022, 12:31am UTC](https://discourse.slimframework.com/t/not-found-handler-without-error-middleware/5317/3 "2022-07-31T00:31:04Z")

</div>

> [@odan](#):
>
> It is also possible to remove the ErrorMiddleware completely, and handle everything yourself.

Thanks for the response. I do want to remove the ErrorMiddleware completely. Actually, I’m not adding it in the first place. Are you saying it’s possible to do that and use Slim’s Not Found Handler? The [Upgrade Guide shows](https://www.slimframework.com/docs/v4/start/upgrade.html) the Not Found Handler being added to the ErrorMiddleware.

```auto
$errorMiddleware = $app->addErrorMiddleware(true, true, true);

// Set the Not Found Handler
$errorMiddleware->setErrorHandler(
    HttpNotFoundException::class,
    function (ServerRequestInterface $request, Throwable $exception, bool $displayErrorDetails) {
        $response = new Response();
        $response->getBody()->write('404 NOT FOUND');
        return $response->withStatus(404);
    });

```

---

<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:** [July 31, 2022, 10:13am UTC](https://discourse.slimframework.com/t/not-found-handler-without-error-middleware/5317/4 "2022-07-31T10:13:35Z")

</div>

Yes, this is actually the “recommend” way to handle the Not Found error.

---

<div class="post-metadata">

**Author:** ![dbcl](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@dbcl](https://discourse.slimframework.com/u/dbcl)\
**Post date:** [August 1, 2022, 1:39pm UTC](https://discourse.slimframework.com/t/not-found-handler-without-error-middleware/5317/5 "2022-08-01T13:39:09Z")

</div>

I’m confused, as it seems to require ErrorMiddleware.

---

<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:** [August 1, 2022, 9:02pm UTC](https://discourse.slimframework.com/t/not-found-handler-without-error-middleware/5317/6 "2022-08-01T21:02:48Z")

</div>

If you add the ErrorMiddleware then you can add custom handlers (functions or invokable classes) to the Error Middleware using the `setErrorHandler` method. This is the recommended way.

If you don’t want to add the ErrorMiddleware, then you need to add a custom ErrorMiddleware to the stack, that catches and handles the specific exceptions, e.g. the `HttpNotFoundException` etc.

The 3. option is a combination of both, one or more custom ErrorMiddlewares + the Slim ErrorMiddleware as “fallback” for all other errors.

---

<div class="post-metadata">

**Author:** ![dbcl](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@dbcl](https://discourse.slimframework.com/u/dbcl)\
**Post date:** [August 2, 2022, 12:29pm UTC](https://discourse.slimframework.com/t/not-found-handler-without-error-middleware/5317/7 "2022-08-02T12:29:29Z")

</div>

Thanks a lot for the explanation. I’ve managed to get a custom 404 working from .htaccess. There’s a small problem I may ignore. Plus, there are benefits of handling from Slim like writing request info to a db events table. So, I may play around with setting my error handler as Slim’s ErrorMiddleware.
