diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index c2839628f..742a6a6ad 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -18,6 +18,7 @@ use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface; use Chill\DocGeneratorBundle\Context\Exception\ContextNotFoundException; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\GeneratorDriver\DriverInterface; +use Chill\DocGeneratorBundle\GeneratorDriver\Exception\TemplateException; use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\MainBundle\Pagination\PaginatorFactory; @@ -276,7 +277,15 @@ final class DocGeneratorTemplateController extends AbstractController $datas = $context->getData($template, $entity, $contextGenerationData); dump('datas compiled', $datas); - $generatedResource = $this->driver->generateFromResource($templateResource, $template->getFile()->getType(), $datas, $template->getFile()->getFilename()); + try { + $generatedResource = $this->driver->generateFromResource($templateResource, $template->getFile()->getType(), $datas, $template->getFile()->getFilename()); + } catch (TemplateException $e) { + $msg = implode("\n", $e->getErrors()); + + return new Response($msg, 400, [ + 'Content-Type' => 'text/plain', + ]); + } fclose($templateResource); diff --git a/src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/Exception/TemplateException.php b/src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/Exception/TemplateException.php new file mode 100644 index 000000000..38f71cbab --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/Exception/TemplateException.php @@ -0,0 +1,34 @@ +errors = $errors; + } + + public function getErrors(): array + { + return $this->errors; + } +} diff --git a/src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/RelatorioDriver.php b/src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/RelatorioDriver.php index 8889e72b1..7a3e4ac69 100644 --- a/src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/RelatorioDriver.php +++ b/src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/RelatorioDriver.php @@ -11,6 +11,7 @@ declare(strict_types=1); namespace Chill\DocGeneratorBundle\GeneratorDriver; +use Chill\DocGeneratorBundle\GeneratorDriver\Exception\TemplateException; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\Mime\Part\DataPart; @@ -54,10 +55,21 @@ class RelatorioDriver implements DriverInterface return $response->toStream(); } catch (HttpExceptionInterface $e) { + $content = $e->getResponse()->getContent(false); + + if (400 === $e->getResponse()->getStatusCode()) { + $content = json_decode($content, true); + $this->logger->error('relatorio: template error', [ + 'error' => $content['message'] ?? '_not defined', + ]); + + throw new TemplateException([$content['message']]); + } + $this->logger->error('relatorio: error while generating document', [ 'msg' => $e->getMessage(), 'response' => $e->getResponse()->getStatusCode(), - 'content' => $e->getResponse()->getContent(false), + 'content' => $content, ]); throw $e;