public function onKernelException(GetResponseForExceptionEvent $event) { $e = $event->getException(); $statusCode = $e instanceof HttpExceptionInterface ? $e->getStatusCode() : 500; // allow 500 errors to be thrown if ($this->debug && $statusCode >= 500) { return; } if ($e instanceof ApiProblemException) { $apiProblem = $e->getApiProblem(); } else { $apiProblem = new ApiProblem($statusCode); /* * If it's an HttpException message (e.g. for 404, 403), * we'll say as a rule that the exception message is safe * for the client. Otherwise, it could be some sensitive * low-level exception, which should *not* be exposed */ if ($e instanceof HttpExceptionInterface) { $apiProblem->set('detail', $e->getMessage()); } } $data = $apiProblem->toArray(); $response = new JsonResponse($data, $apiProblem->getStatusCode()); $response->headers->set('Content-Type', 'application/json'); $event->setResponse($response); }
/** @test */ public function it_sorts_the_response_data() { $response = new ApiProblem(404, null, ['my_stuff' => 'comes here']); $response->set('meta', 'will be here'); $response->set('additional_data', 'will be here'); $data = $response->toArray(); $keys = array_keys($data); $this->assertEquals('status', $keys[0]); $this->assertEquals('type', $keys[1]); $this->assertEquals('title', $keys[2]); $this->assertEquals('data', $keys[3]); $this->assertEquals('additional_data', $keys[4]); $this->assertEquals('meta', $keys[5]); }