Skip to content

Commit ea6bae2

Browse files
committed
Add support for configuration of graceful exception handling for serialization process.
1 parent dfb1cba commit ea6bae2

File tree

4 files changed

+84
-8
lines changed

4 files changed

+84
-8
lines changed

Classes/Serializer/Accessor/AccessorStrategy.php

+26-8
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,35 @@ public function __construct()
3434
*/
3535
public function getValue(object $object, PropertyMetadata $metadata, SerializationContext $context)
3636
{
37-
if ($metadata instanceof ExpressionPropertyMetadata) {
38-
$variables = ['object' => $object, 'context' => $context, 'property_metadata' => $metadata];
37+
try {
38+
if ($metadata instanceof ExpressionPropertyMetadata) {
39+
$variables = ['object' => $object, 'context' => $context, 'property_metadata' => $metadata];
40+
return $this->evaluator->evaluate((string)($metadata->expression), $variables);
41+
}
3942

40-
return $this->evaluator->evaluate((string)($metadata->expression), $variables);
41-
}
43+
if ($metadata->getter === null) {
44+
return ObjectAccess::getProperty($object, $metadata->name);
45+
}
46+
return $object->{$metadata->getter}();
4247

43-
if ($metadata->getter === null) {
44-
return ObjectAccess::getProperty($object, $metadata->name, false);
48+
} catch (\Exception $exception) {
49+
$exclusionForExceptions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['serializer']['exclusionForExceptionsInAccessorStrategyGetValue'];
50+
foreach ($exclusionForExceptions as $objectClass => $exceptionClasses) {
51+
if ($object instanceof $objectClass) {
52+
if (in_array('*', $exceptionClasses, true)) {
53+
trigger_error($exception->getMessage(), E_USER_WARNING);
54+
return null;
55+
}
56+
foreach ($exceptionClasses as $exceptionClass) {
57+
if ($exception instanceof $exceptionClass) {
58+
trigger_error($exception->getMessage(), E_USER_WARNING);
59+
return null;
60+
}
61+
}
62+
}
63+
}
64+
throw $exception;
4565
}
46-
47-
return $object->{$metadata->getter}();
4866
}
4967

5068
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.. include:: ../../Includes.txt
2+
3+
4+
.. _serialization_exceptions:
5+
6+
==========
7+
Exceptions
8+
==========
9+
10+
TYPO3 may encounter issues with FileReference or File objects, such as when a file
11+
is missing, inaccessible, or if the relation is broken. These issues can interrupt
12+
the serialization process and a jsonified error will be returned.
13+
To address this, we've introduced a configuration option that allows for the
14+
graceful handling of specific exceptions during the serialization process.
15+
This configuration is defined on a per-class basis, meaning that different
16+
classes can have different sets of exceptions that are handled gracefully.
17+
18+
Once configured, these exceptions will not interrupt the serialization process
19+
for the respective class. Instead, they will be handled appropriately,
20+
allowing the serialization process to continue uninterrupted.
21+
This ensures that a problem with a single object does not prevent the successful
22+
serialization of other objects.
23+
24+
25+
Setting responsible for that is:
26+
27+
.. code-block:: php
28+
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['serializer']['exclusionForExceptionsInAccessorStrategyGetValue']
29+
30+
31+
Example value:
32+
33+
.. code-block:: php
34+
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['serializer']['exclusionForExceptionsInAccessorStrategyGetValue'] = [
35+
\SourceBroker\T3apinews\Domain\Model\FileReference::class => [
36+
\TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException::class,
37+
],
38+
];
39+
40+
Asterix as "all exceptions" is also supported:
41+
Example:
42+
43+
.. code-block:: php
44+
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['serializer']['exclusionForExceptionsInAccessorStrategyGetValue'] = [
45+
\SourceBroker\T3apinews\Domain\Model\FileReference::class => ['*'],
46+
];

Documentation/Serialization/Index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ Serialization
1717
Subscribers/Index
1818
YamlMetadata/Index
1919
Customization/Index
20+
Exceptions/Index

ext_localconf.php

+11
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ static function () {
104104
];
105105
}
106106

107+
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['serializer']['exclusionForExceptionsInAccessorStrategyGetValue'] = [
108+
TYPO3\CMS\Core\Resource\FileReference::class => [
109+
\TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException::class,
110+
\UnexpectedValueException::class,
111+
],
112+
TYPO3\CMS\Extbase\Domain\Model\FileReference::class => [
113+
\TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException::class,
114+
\UnexpectedValueException::class,
115+
],
116+
];
117+
107118
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['clearCachePostProc']['t3api_clearcache'] =
108119
\SourceBroker\T3api\Service\SerializerService::class . '->clearCache';
109120

0 commit comments

Comments
 (0)