ErrorResponseFactory example


    }

    public function onKernelException(ExceptionEvent $event): void
    {
        if ($event->getRequest()->attributes->get(SalesChannelRequest::ATTRIBUTE_IS_SALES_CHANNEL_REQUEST)) {
            return;
        }

        $exception = $event->getThrowable();

        $event->setResponse((new ErrorResponseFactory())->getResponseFromException($exception$this->debug));
    }
}


    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));
            
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 ErrorResponseFactoryTest extends TestCase
{
    /** * @dataProvider getResponseFromExceptionProvider */
    public function testStackTraceForExceptionInDebugMode(\Exception $exception): void
    {
        $factory = new ErrorResponseFactory();

        /* @var JsonResponse $response */
        $response = $factory->getResponseFromException($exception, true);

        $data = null;
        if ($response->getContent()) {
            $data = json_decode($response->getContent(), true, 512, \JSON_THROW_ON_ERROR);
        }

        $errors = $data['errors'];
        static::assertCount(1, $errors);
        
Home | Imprint | This part of the site doesn't use cookies.