# Custom Error Handler Error 500

**URL:** <https://discourse.slimframework.com/t/custom-error-handler-error-500/4496>\
**Category:** Questions\
**Created:** [November 9, 2020, 3:26pm UTC](https://discourse.slimframework.com/t/custom-error-handler-error-500/4496 "2020-11-09T15:26:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![basusingh](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/basusingh/32/1237_2.png) [@basusingh](https://discourse.slimframework.com/u/basusingh)\
**Post date:** [November 9, 2020, 3:26pm UTC](https://discourse.slimframework.com/t/custom-error-handler-error-500/4496/1 "2020-11-09T15:26:16Z")

</div>

I have added a custom error handler to my backend. However, I am getting 500 Internal Server Error.  
Here’s my code:

> $errorHandler = new HttpErrorHandler($callableResolver, $responseFactory);  
> $app-\>addRoutingMiddleware();  
> $errorMiddleware = $app-\>addErrorMiddleware($displayErrorDetails, $logErrors, $logErrorDetails);  
> $errorMiddleware-\>setDefaultErrorHandler($customErrorHandler);

My HttpErrorHandler

> class HttpErrorHandler extends ErrorHandler  
> {  
> public const BAD\_REQUEST = ‘BAD\_REQUEST’;  
> public const INSUFFICIENT\_PRIVILEGES = ‘INSUFFICIENT\_PRIVILEGES’;  
> public const NOT\_ALLOWED = ‘NOT\_ALLOWED’;  
> public const NOT\_IMPLEMENTED = ‘NOT\_IMPLEMENTED’;  
> public const RESOURCE\_NOT\_FOUND = ‘RESOURCE\_NOT\_FOUND’;  
> public const SERVER\_ERROR = ‘SERVER\_ERROR’;  
> public const UNAUTHENTICATED = ‘UNAUTHENTICATED’;  
> protected function logError(string $error): void  
> {  
> //TODO  
> // Insert custom error logging function.  
> }

```
protected function respond(): ResponseInterface
{
    $exception = $this->exception;
    $statusCode = 500;
    $type = self::SERVER_ERROR;
    $description = 'An internal error has occurred while processing your request.';

    if ($exception instanceof HttpException) {
        $statusCode = $exception->getCode();
        $description = $exception->getMessage();

        if ($exception instanceof HttpNotFoundException) {
            $type = self::RESOURCE_NOT_FOUND;
        } elseif ($exception instanceof HttpMethodNotAllowedException) {
            $type = self::NOT_ALLOWED;
        } elseif ($exception instanceof HttpUnauthorizedException) {
            $type = self::UNAUTHENTICATED;
        } elseif ($exception instanceof HttpForbiddenException) {
            $type = self::UNAUTHENTICATED;
        } elseif ($exception instanceof HttpBadRequestException) {
            $type = self::BAD_REQUEST;
        } elseif ($exception instanceof HttpNotImplementedException) {
            $type = self::NOT_IMPLEMENTED;
        }
    }

    if (
        !($exception instanceof HttpException)
        && ($exception instanceof Exception || $exception instanceof Throwable)
        && $this->displayErrorDetails
    ) {
        $description = $exception->getMessage();
    }

    $error = [
        'statusCode' => $statusCode,
        'error' => [
            'type' => $type,
            'description' => $description,
        ],
    ];
    
    $payload = json_encode($error, JSON_PRETTY_PRINT);
    
    $response = $this->responseFactory->createResponse($statusCode);        
    $response->getBody()->write($payload);
    
    return $response;
}

```

}

What am I doing wrong?  
My requirement here is pretty simple: Everytime there’s an error (404, 500 or similar), I want to render a common web page with different status code and message each time. I couldn’t figure out how to catch those errors automatically.

---

<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:** [November 10, 2020, 7:40am UTC](https://discourse.slimframework.com/t/custom-error-handler-error-500/4496/2 "2020-11-10T07:40:10Z")

</div>

Have you tried this `HttpExceptionMiddleware` ?

- [https://odan.github.io/2020/05/27/slim4-error-handling.html](https://odan.github.io/2020/05/27/slim4-error-handling.html)

---

<div class="post-metadata">

**Author:** ![basusingh](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/basusingh/32/1237_2.png) [@basusingh](https://discourse.slimframework.com/u/basusingh)\
**Post date:** [November 10, 2020, 4:50pm UTC](https://discourse.slimframework.com/t/custom-error-handler-error-500/4496/3 "2020-11-10T16:50:20Z")

</div>

Hi Odan. Thank you for your reply. I am definitely not experienced with Php and slim. Could you help me understand how can I implement this line into my code:  
`$app->add(HttpExceptionMiddleware::class);`  
This is my current code, but it is showing Slim application error:

```
    $app->addRoutingMiddleware();
    $app->add(HttpExceptionMiddleware::class);
    $errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, $logErrors, $logErrorDetails);
```

---

<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:** [November 10, 2020, 5:43pm UTC](https://discourse.slimframework.com/t/custom-error-handler-error-500/4496/4 "2020-11-10T17:43:20Z")

</div>

You will find step-by-step instructions within the article.  
Do you have installed a dependency injection container, like [PHP-DI](https://odan.github.io/2020/05/24/slim4-php-di.html)?  
If not, you have to instantiate the objects manually.

---

<div class="post-metadata">

**Author:** ![basusingh](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/basusingh/32/1237_2.png) [@basusingh](https://discourse.slimframework.com/u/basusingh)\
**Post date:** [November 11, 2020, 4:42am UTC](https://discourse.slimframework.com/t/custom-error-handler-error-500/4496/5 "2020-11-11T04:42:24Z")

</div>

Hi odan.  
I don’t have any dependency injection container. I’m trying this code to manually instantiate the object:

` $app->addRoutingMiddleware();`  
`$responseFactory = $app->getResponseFactory();`  
`$app->add(new HttpExceptionMiddleware($responseFactory));`  
`$errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, $logErrors, $logErrorDetails);`

It is definitely not working. Same 500 error. I guess this is not how I should instantiate the class  
`new HttpExceptionMiddleware($responseFactory)`  
How can I do it manually?

---

<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:** [November 11, 2020, 7:38am UTC](https://discourse.slimframework.com/t/custom-error-handler-error-500/4496/6 "2020-11-11T07:38:58Z")

</div>

The code looks OK. What is the exact error message? Do you have xdebug installed to debug it?  
In the long run you need a dependency injection container anyway. I really recommend installing it.

---

<div class="post-metadata">

**Author:** ![basusingh](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/basusingh/32/1237_2.png) [@basusingh](https://discourse.slimframework.com/u/basusingh)\
**Post date:** [November 17, 2020, 6:09pm UTC](https://discourse.slimframework.com/t/custom-error-handler-error-500/4496/7 "2020-11-17T18:09:05Z")

</div>

Hi Odan. I don’t get any error message. It’s just a blank page with status code 500.

I will try to install a dependency injection container.

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [November 18, 2020, 12:15am UTC](https://discourse.slimframework.com/t/custom-error-handler-error-500/4496/8 "2020-11-18T00:15:24Z")

</div>

The 500 status code indicates that your web server generated the error, so you should find it in your server (Apache, Nginx, etc) log.
