Custom Error Handler Error 500

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);
$errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, $logErrors, $logErrorDetails);

My HttpErrorHandler

class HttpErrorHandler extends ErrorHandler
public const BAD_REQUEST = ‘BAD_REQUEST’;
public const NOT_ALLOWED = ‘NOT_ALLOWED’;
protected function logError(string $error): void
// 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);        
    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.

Have you tried this HttpExceptionMiddleware ?

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:
This is my current code, but it is showing Slim application error:

    $errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, $logErrors, $logErrorDetails);

You will find step-by-step instructions within the article.
Do you have installed a dependency injection container, like PHP-DI?
If not, you have to instantiate the objects manually.

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

$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?

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.

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.

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