Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
15.38% |
4 / 26 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| AppExceptionRenderer | |
15.38% |
4 / 26 |
|
0.00% |
0 / 1 |
4.42 | |
0.00% |
0 / 1 |
| render | |
15.38% |
4 / 26 |
|
0.00% |
0 / 1 |
4.42 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace App\Error; |
| 5 | |
| 6 | use App\Http\Exception\TooManyRequestsException; |
| 7 | use Cake\Error\Renderer\WebExceptionRenderer; |
| 8 | use Psr\Http\Message\ResponseInterface; |
| 9 | |
| 10 | /** |
| 11 | * Class AppExceptionRenderer |
| 12 | * |
| 13 | * Custom exception renderer for handling specific application exceptions. |
| 14 | * Extends the WebExceptionRenderer to provide custom rendering logic for exceptions. |
| 15 | */ |
| 16 | class AppExceptionRenderer extends WebExceptionRenderer |
| 17 | { |
| 18 | /** |
| 19 | * Render method to handle exceptions and generate a response. |
| 20 | * |
| 21 | * This method checks if the exception is an instance of TooManyRequestsException. |
| 22 | * If so, it sets the response status code, prepares the view template, and sets |
| 23 | * various data to be serialized in the response. Otherwise, it falls back to the |
| 24 | * parent render method. |
| 25 | * |
| 26 | * @return \Psr\Http\Message\ResponseInterface The response object with the rendered error page. |
| 27 | */ |
| 28 | public function render(): ResponseInterface |
| 29 | { |
| 30 | $exception = $this->error; |
| 31 | $code = $this->getHttpCode($exception); |
| 32 | |
| 33 | if ($exception instanceof TooManyRequestsException) { |
| 34 | $this->controller->setResponse($this->controller->getResponse()->withStatus($code)); |
| 35 | $this->controller->viewBuilder()->setTemplatePath('Error'); |
| 36 | |
| 37 | $message = $exception->getMessage(); |
| 38 | $url = $this->controller->getRequest()->getRequestTarget(); |
| 39 | |
| 40 | $this->controller->set([ |
| 41 | 'message' => $message, |
| 42 | 'url' => h($url), |
| 43 | 'error' => $exception, |
| 44 | 'code' => $code, |
| 45 | 'exceptions' => [$exception], |
| 46 | 'file' => $exception->getFile(), |
| 47 | 'line' => $exception->getLine(), |
| 48 | ]); |
| 49 | |
| 50 | $this->controller->viewBuilder()->setOption('serialize', [ |
| 51 | 'message', |
| 52 | 'url', |
| 53 | 'code', |
| 54 | 'exceptions', |
| 55 | 'file', |
| 56 | 'line', |
| 57 | ]); |
| 58 | |
| 59 | return $this->_outputMessage('error429'); |
| 60 | } |
| 61 | |
| 62 | return parent::render(); |
| 63 | } |
| 64 | } |