# Error handler based on route

**URL:** <https://discourse.slimframework.com/t/error-handler-based-on-route/5902>\
**Category:** Questions\
**Created:** [May 14, 2024, 1:37pm UTC](https://discourse.slimframework.com/t/error-handler-based-on-route/5902 "2024-05-14T13:37:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![thierryler](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/thierryler/32/1115_2.png) [@thierryler](https://discourse.slimframework.com/u/thierryler)\
**Post date:** [May 14, 2024, 1:37pm UTC](https://discourse.slimframework.com/t/error-handler-based-on-route/5902/1 "2024-05-14T13:37:58Z")

</div>

Hello.

I’ve setup an Error Middleware in middlewares.php:

```auto
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
$errorMiddleware->setDefaultErrorHandler($container->get(DefaultErrorHandler::class));

```

with DefaultErrorHandler copied from [https://github.com/odan/slim4-skeleton/blob/master/src/Handler/DefaultErrorHandler.php](https://github.com/odan/slim4-skeleton/blob/master/src/Handler/DefaultErrorHandler.php)

And it works and return html errors.

But for some routes (api routes), I’d like to have json errors, something like:

```auto
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
$errorHandler = $errorMiddleware->getDefaultErrorHandler();
$errorHandler->forceContentType('application/json);

```

But I do not find how to setup that in my routes:

```auto
$app->group('/api', function (Group $groupApi) {
    $groupApi->get('/dogs', DogApiController::class. ':getDogs')->setName('api-dogs');
 })->add(ApiErrorMiddleware::class);

```

I’ve tried that:

```auto
class ApiErrorMiddleware implements Middleware
{

    private $logger;
    private $app;

    public function __construct(Logger $logger, App $app)
    {
        $this->logger = $logger;
        $this->app = $app;
    }

    public function process(Request $request, RequestHandler $handler): Response
    {
        $this->logger->debug("...");

        $errorMiddleware = $this->app->addErrorMiddleware(true, true, true);
        $errorHandler = $errorMiddleware->getDefaultErrorHandler();
        $errorHandler->forceContentType('application/json');

        return $handler->handle($request);
    }
}

```

But it has no effect ☹

is there a way to replace the handler from a 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:** [May 14, 2024, 4:31pm UTC](https://discourse.slimframework.com/t/error-handler-based-on-route/5902/2 "2024-05-14T16:31:33Z")

</div>

Note that this middleware is optional, so you do not need to use the Slim error handler at all. If you have some special requirements, then you may also add your own ErrorHandler middleware that does content negotiation on it’s own, see [here](https://github.com/odan/slim4-skeleton/blob/master/src/Middleware/ExceptionMiddleware.php#L68).

If you still want to use the Slim error handler middleware with the integrated content negotiation, you could register a error render for each content-type using the `registerErrorRenderer` method. Example:

```php
// Add Error Middleware
$errorMiddleware = $app->addErrorMiddleware(true, true, true);

// Get the default error handler and register custom error renderers
$errorHandler = $errorMiddleware->getDefaultErrorHandler();
$errorHandler->registerErrorRenderer('text/html', HtmlErrorRenderer::class);
$errorHandler->registerErrorRenderer('application/json', JsonErrorRenderer::class);
// ...

$app->run();

```

For any content type you can register your own error renderer. So first define a new error renderer that implements `\Slim\Interfaces\ErrorRendererInterface`.

```auto
<?php

use App\Middleware;

use Slim\Interfaces\ErrorRendererInterface;
use Throwable;

final class HtmlErrorRenderer implements ErrorRendererInterface
{
    public function __invoke(Throwable $exception, bool $displayErrorDetails): string
    {
        return 'My HTML output';
    }
}

```

Read more: [Error Middleware - Slim Framework](https://www.slimframework.com/docs/v4/middleware/error-handling.html#error-handlingrendering)
