/**
  * @inheritdoc
  */
 public function parse(Exception $e)
 {
     if ($e instanceof JsonApiException) {
         $errors = $e->getErrors();
         $httpCode = $e->getHttpCode();
     } else {
         $errors = $this->getErrors($e);
         $httpCode = $this->getDefaultHttpCode($e);
     }
     $errors = Errors::cast($errors);
     /** @var MutableErrorInterface $error */
     foreach ($errors as $error) {
         $this->assignId($error, $e);
     }
     return new ErrorResponse($errors, $httpCode, $this->getHeaders($e));
 }
Exemplo n.º 2
0
 /**
  * ErrorResponse constructor.
  * @param ErrorInterface|ErrorInterface[]|ErrorCollection $errors
  * @param int|null $defaultHttpCode
  * @param array $headers
  */
 public function __construct($errors, $defaultHttpCode = null, array $headers = [])
 {
     $this->errors = Errors::cast($errors);
     $this->defaultHttpCode = $defaultHttpCode;
     $this->headers = $headers;
 }
 /**
  * @inheritdoc
  */
 public function relationshipNotAcceptable(ResourceIdentifierInterface $identifier, $relationshipKey = null, $error = null)
 {
     $base = $this->repository->errorWithPointer(self::RELATIONSHIP_NOT_ACCEPTABLE, $relationshipKey ? P::relationship($relationshipKey) : P::data(), ['type' => $identifier->getType(), 'id' => $identifier->getId()]);
     $errors = new MutableErrorCollection();
     /** @var MutableErrorInterface $err */
     foreach (MutableErrorCollection::cast($error ?: $base) as $err) {
         $add = clone $base;
         $errors->add($add->merge($err));
     }
     return $errors;
 }
 /**
  * ValidationException constructor.
  * @param ErrorInterface|ErrorInterface[]|ErrorCollection $errors
  * @param string|int|null $defaultHttpCode
  * @param Exception|null $previous
  */
 public function __construct($errors, $defaultHttpCode = self::DEFAULT_HTTP_CODE, Exception $previous = null)
 {
     $errors = MutableErrorCollection::cast($errors);
     parent::__construct($errors, $errors->getHttpStatus($defaultHttpCode), $previous);
 }
 public function testCastArray()
 {
     $arr = [new Error(null, null, 500)];
     $expected = new MutableErrorCollection($arr);
     $this->assertEquals($expected, MutableErrorCollection::cast($arr));
 }