/** @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]);
 }
 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);
 }
 /**
  * Validate json decoding
  */
 protected function validateJsonContent()
 {
     switch (json_last_error()) {
         case JSON_ERROR_DEPTH:
             $error = 'Maximum stack depth exceeded';
             break;
         case JSON_ERROR_CTRL_CHAR:
             $error = 'Unexpected control character found';
             break;
         case JSON_ERROR_SYNTAX:
             $error = 'Syntax error, malformed JSON';
             break;
     }
     if (!empty($error)) {
         $problem = new ApiProblem(400, ApiProblem::TYPE_INVALID_REQUEST_BODY_FORMAT);
         $problem->set('error', $error);
         throw new ApiProblemException($problem);
     }
 }
 /**
  * @param $errors
  * @return ApiProblemException
  */
 protected function createValidationErrorException($errors)
 {
     $problem = new ApiProblem(422, ApiProblem::TYPE_VALIDATION_ERROR);
     $problem->set('errors', $errors);
     return new ApiProblemException($problem);
 }