예제 #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);
 }
예제 #2
0
 public function __construct(ApiProblem $apiProblem, \Exception $previous = null, array $headers = array(), $code = 0)
 {
     $this->apiProblem = $apiProblem;
     parent::__construct($apiProblem->getStatusCode(), $apiProblem->getTitle(), $previous, $headers, $code);
 }
예제 #3
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;
     });
 }