/** * Format response exception * * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); if (!$event->getRequest() || $this->matchAll === false && !$this->isApiException($exception)) { return; } if ($this->isApiException($exception)) { $exception = $this->exceptionManager->configure($exception); } $statusCode = $this->getStatusCode($exception); $data['error']['status'] = $statusCode; $data['error']['code'] = $exception->getCode(); $data['error']['message'] = $this->getMessage($exception); if ($this->isFlattenErrorException($exception)) { $data['error']['errors'] = $exception->getFlattenErrors(); } if ($this->stackTrace) { $data['error']['stack_trace'] = $exception->getTrace(); } $response = new JsonResponse($data, $statusCode, $this->getHeaders($exception)); $event->setResponse($response); }
public function testConfigure() { $this->given($defaultConfig = $this->getDefaultConfig(), $exceptionsConfig = $this->getExceptionsConfig(), $badRequestException = new BadRequestException(), $validationFormException = new ValidationFormException($this->getFormMock()), $typeNotFoundException = new TypeNotFoundHttpException(), $exceptionManager = new TestedClass($defaultConfig, $exceptionsConfig))->then->object($exception = $exceptionManager->configure($badRequestException))->isInstanceOf('M6Web\\Bundle\\ApiExceptionBundle\\Exception\\BadRequestException')->integer($exception->getCode())->isEqualTo($defaultConfig['code'])->string($exception->getMessage())->isEqualTo($defaultConfig['message'])->object($exception = $exceptionManager->configure($validationFormException))->isInstanceOf('M6Web\\Bundle\\ApiExceptionBundle\\Exception\\ValidationFormException')->integer($exception->getCode())->isEqualTo($exceptionsConfig['M6Web\\Bundle\\ApiExceptionBundle\\Exception\\ValidationFormException']['code'])->string($exception->getMessage())->isEqualTo($exceptionsConfig['M6Web\\Bundle\\ApiExceptionBundle\\Exception\\ValidationFormException']['message'])->integer($exception->getStatusCode())->isEqualTo($defaultConfig['status'])->array($headers = $exception->getHeaders())->hasSize(1)->string($headers['Exception'])->isEqualTo($exceptionsConfig['M6Web\\Bundle\\ApiExceptionBundle\\Exception\\ValidationFormException']['headers']['Exception'])->object($exception = $exceptionManager->configure($typeNotFoundException))->isInstanceOf('M6Web\\Bundle\\ApiExceptionBundle\\Tests\\Fixtures\\Exception\\TypeNotFoundHttpException')->integer($exception->getCode())->isEqualTo($exceptionsConfig['M6Web\\Bundle\\ApiExceptionBundle\\Tests\\Fixtures\\Exception\\TypeNotFoundHttpException']['code'])->string($exception->getMessage())->isEqualTo($exceptionsConfig['M6Web\\Bundle\\ApiExceptionBundle\\Tests\\Fixtures\\Exception\\TypeNotFoundHttpException']['message'])->integer($exception->getStatusCode())->isEqualTo($exceptionsConfig['M6Web\\Bundle\\ApiExceptionBundle\\Tests\\Fixtures\\Exception\\TypeNotFoundHttpException']['status'])->array($exception->getHeaders())->hasSize(0)->isEqualTo($defaultConfig['headers']); }