handle JSON exception

This commit is contained in:
Daniel Seifert 2021-11-10 13:52:26 +01:00
parent e134a4c14a
commit 5251490b74
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
2 changed files with 10 additions and 6 deletions

View File

@ -16,6 +16,7 @@ declare(strict_types=1);
namespace D3\DataWizard\Application\Model\ExportRenderer;
use D3\DataWizard\Application\Model\Exceptions\RenderException;
use JsonException;
class Json implements RendererInterface
{
@ -28,12 +29,15 @@ class Json implements RendererInterface
*/
public function getContent($rows, $fieldNames): string
{
$flags = JSON_PRETTY_PRINT;
$json = json_encode( $rows, $flags );
if ( $json === false ) {
throw oxNew( RenderException::class, json_last_error_msg(), json_last_error());
try {
$flags = JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR;
$json = json_encode( $rows, $flags );
return $json;
} catch ( JsonException $e) {
/** @var RenderException $newException */
$newException = oxNew(RenderException::class, $e->getMessage(), $e->getCode(), $e );
throw $newException;
}
return $json;
}
public function getFileExtension(): string

View File

@ -25,7 +25,7 @@
"GPL-3.0-or-later"
],
"require": {
"php": ">=7.1",
"php": ">=7.3",
"oxid-esales/oxideshop-ce": "6.3 - 6.9",
"league/csv": "^9.0",
"mathieuviossat/arraytotexttable": "^1.0",