public function __construct(ApiProblem $apiProblem, \Exception $previous = null, array $headers = array(), $code = 0)
 {
     $this->apiProblem = $apiProblem;
     $statusCode = $apiProblem->getStatusCode();
     $message = $apiProblem->getTitle();
     parent::__construct($statusCode, $message, $previous, $headers, $code);
 }
Example #2
0
 private function configureListeners()
 {
     $app = $this;
     $this->error(function (\Exception $e, $statusCode) use($app) {
         // only act on /api URLs
         if (strpos($app['request']->getPathInfo(), '/api') !== 0) {
             return;
         }
         // allow 500 errors in debug to be thrown
         if ($app['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 HttpException) {
                 $apiProblem->set('detail', $e->getMessage());
             }
         }
         $data = $apiProblem->toArray();
         // making type a URL, to a temporarily fake page
         if ($data['type'] != 'about:blank') {
             $data['type'] = 'http://localhost:8000/docs/errors#' . $data['type'];
         }
         $response = new JsonResponse($data, $apiProblem->getStatusCode());
         $response->headers->set('Content-Type', 'application/problem+json');
         return $response;
     });
 }