Ejemplo n.º 1
0
 private function throwApiProblemValidationException(array $errors)
 {
     $apiProblem = new ApiProblem(400, ApiProblem::TYPE_VALIDATION_ERROR);
     $apiProblem->set('errors', $errors);
     $response = new JsonResponse($apiProblem->toArray(), $apiProblem->getStatusCode());
     $response->headers->set('Content-Type', 'application/problem+json');
     throw new ApiProblemException($apiProblem);
 }
Ejemplo n.º 2
0
 private function configureListeners()
 {
     $app = $this;
     $this->error(function (\Exception $e, $statusCode) use($app) {
         if (strpos($app['request']->getPathInfo(), '/api') !== 0) {
             return;
         }
         if ($app['debug'] && $statusCode === 500) {
             return;
         }
         if ($e instanceof ApiProblemException) {
             $apiProblem = $e->getApiProblem();
         } else {
             $apiProblem = new ApiProblem($statusCode);
             if ($e instanceof HttpException) {
                 $apiProblem->set('detail', $e->getMessage());
             }
         }
         $data = $apiProblem->toArray();
         if ($data['type'] != 'about:blank') {
             $data['type'] = 'http://forbiddencat.local/api/docs/errors#' . $data['type'];
         }
         $response = new JsonResponse($data, $statusCode);
         $response->headers->set('Content-Type', 'application/problem+json');
         return $response;
     });
 }