The middleware catches the exception thrown, and returns a response object based based on the code and the requested format.
```php
public function afterException($controller, $methodName, \Exception $exception){
    if($exception instanceof HttpException) {
        $response = null;
        if($this->request-> /* however the format is requested */  === 'json') {
            $response = new JSONResponse();
        } elseif(/* 'xml' */) {
            $response = new XmlResponse(/* yada yada */);
        } else {
            ...
        }
        $response->setStatus($exception->getCode());
        $response->setErrorMessage($exception->getMessage());
  }
  ...
}
```