show errors from relatorio driver

This commit is contained in:
Julien Fastré 2021-12-09 21:50:56 +01:00
parent 75ba56fa09
commit e266fa0e5d
3 changed files with 57 additions and 2 deletions

View File

@ -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);

View File

@ -0,0 +1,34 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\DocGeneratorBundle\GeneratorDriver\Exception;
use RuntimeException;
/**
* A exception to throw when there are syntax errors in the
* template file.
*/
class TemplateException extends RuntimeException
{
private array $errors;
public function __construct(array $errors, $code = 0, ?Throwable $previous = null)
{
parent::__construct('Error while generating document from template', $code, $previous);
$this->errors = $errors;
}
public function getErrors(): array
{
return $this->errors;
}
}

View File

@ -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;