# 404 and CustomErrorHandler

**URL:** <https://discourse.slimframework.com/t/404-and-customerrorhandler/6014>\
**Category:** Questions\
**Created:** [December 9, 2024, 11:11pm UTC](https://discourse.slimframework.com/t/404-and-customerrorhandler/6014 "2024-12-09T23:11:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![EgorKa](https://avatars.discourse-cdn.com/v4/letter/e/94ad74/32.png) [@EgorKa](https://discourse.slimframework.com/u/EgorKa)\
**Post date:** [December 9, 2024, 11:11pm UTC](https://discourse.slimframework.com/t/404-and-customerrorhandler/6014/1 "2024-12-09T23:11:10Z")

</div>

I am registering a custom error handler:

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

$errorHandler = $errorMiddleware->getDefaultErrorHandler();
$errorHandler->registerErrorRenderer('text/html', MyCustomErrorRenderer::class);

```

And I have a custom 404 handler:

```auto
class CustomNotFoundErrorRenderer extends HttpSpecializedException {
...
}

```

But when I do

```auto
throw new CustomNotFoundErrorRenderer($request);

```

my 404 class does not work, but outputs what it returns MyCustomErrorRenderer.

I’m already confused about who intercepts when and what. I need my own error handler and my own 404 page. Please help me.

---

<div class="post-metadata">

**Author:** ![EgorKa](https://avatars.discourse-cdn.com/v4/letter/e/94ad74/32.png) [@EgorKa](https://discourse.slimframework.com/u/EgorKa)\
**Post date:** [December 10, 2024, 6:47pm UTC](https://discourse.slimframework.com/t/404-and-customerrorhandler/6014/2 "2024-12-10T18:47:01Z")

</div>

It’s OK! I used the code recommended in the Upgrade section (I hadn’t looked into this section before as a complete idiot 🤦‍♂️)

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

```

Now everything is working as it should! 🙃
