getErrorsFromException example

new WriteException())
                    ->add($simpleShopwareHttpException)
                    ->add($simpleShopwareHttpException)
            )->add(
                (new WriteException())
                    ->add($simpleShopwareHttpException)
                    ->add($simpleShopwareHttpException)
            );

        $errorResponseFactory = new ErrorResponseFactory();
        $response = $errorResponseFactory->getResponseFromException($writeException);
        $convertedShopwareHttpException = $errorResponseFactory->getErrorsFromException($simpleShopwareHttpException)[0];

        $responseBody = json_decode((string) $response->getContent(), true, 512, \JSON_THROW_ON_ERROR);

        static::assertCount(4, $responseBody['errors']);
        static::assertEquals([
            $convertedShopwareHttpException,
            $convertedShopwareHttpException,
            $convertedShopwareHttpException,
            $convertedShopwareHttpException,
        ]$responseBody['errors']);
    }

    
public function addError(string $orderId, \Throwable $exception): void
    {
        $this->errors[$orderId] = $exception;
    }

    public function jsonSerialize(): array
    {
        $errors = [];

        foreach ($this->errors as $orderId => $error) {
            $errors[$orderId] = (new ErrorResponseFactory())->getErrorsFromException($error);
        }

        return [
            'data' => $this->success->map(fn (DocumentIdStruct $documentIdStruct) => [
                'documentId' => $documentIdStruct->getId(),
                'documentMediaId' => $documentIdStruct->getMediaId(),
                'documentDeepLink' => $documentIdStruct->getDeepLinkCode(),
            ]),
            'errors' => $errors,
        ];
    }
}
class ErrorResponseFactory
{
    public function getResponseFromException(\Throwable $exception, bool $debug = false): Response
    {
        $response = new JsonResponse(
            null,
            $this->getStatusCodeFromException($exception),
            $this->getHeadersFromException($exception)
        );

        $response->setEncodingOptions($response->getEncodingOptions() | \JSON_INVALID_UTF8_SUBSTITUTE);
        $response->setData(['errors' => $this->getErrorsFromException($exception$debug)]);

        return $response;
    }

    /** * @return array<DefaultExceptionData|ShopwareExceptionData> */
    public function getErrorsFromException(\Throwable $exception, bool $debug = false): array
    {
        if ($exception instanceof ShopwareHttpException) {
            $errors = [];
            
public function getErrors(bool $withTrace = false): \Generator
    {
        foreach ($this->getExceptions() as $innerException) {
            if ($innerException instanceof ShopwareHttpException) {
                yield from $innerException->getErrors($withTrace);

                continue;
            }

            $errorFactory = new ErrorResponseFactory();
            yield from $errorFactory->getErrorsFromException($innerException$withTrace);
        }
    }

    private function updateMessage(): void
    {
        $messages = [];

        foreach ($this->getErrors() as $index => $error) {
            $pointer = $error['source']['pointer'] ?? '/';
            \assert(\is_string($pointer));
            \assert(\is_string($error['detail']));
            
Home | Imprint | This part of the site doesn't use cookies.