code: public function exceptionHandler($exception)
      {
          // Try and get the latest request, or a new request
          $request =
              (isset($this->container['latestRequest']) ?
                  $this->container['latestRequest'] : $this->container['request']);
  
          // Try and get the latest response, or a new response
          $response =
              (isset($this->container['latestResponse']) ?
                  $this->container['latestResponse'] : $this->container['response']);
  
          // Check if exception is an instance of the Phapi Exception. If not, create
          // an InternalServerError Exception to get better error message to send to
          // the client. This will also catch PHP 7 ErrorExceptions 
          if (!($exception instanceof Exception)) {
              $exception = new InternalServerError(
                  $exception->getMessage(),
                  $exception->getCode(),
                  $exception->getPrevious()
              );
          }
  
          // Add to log
          $this->logException($exception);
  
          // Prepare response with updated body (error message)
          // Reset body
          $response = $response->withBody(new Stream('php://memory', 'w+'));
          // Set status code
          $response = $response->withStatus($exception->getStatusCode());
          // Set error message
          $body = $this->prepareErrorBody($exception);
  
          // Set body to response
          $response = $response->withUnserializedBody($body);
  
          // Restart pipeline
          $this->container['pipeline']->prepareErrorQueue();
          $this->container['pipeline']($request, $response);
      }
docs: Handle thrown and uncaught exceptions thrown in the middleware
  queue. The exception handler is registered when this Mistake
  middleware is created. Any exceptions thrown before that won't
  be caught by this handler.
  
  @param $exception