Actions
Task #82420
closedIntroduce a standard way of throwing exceptions
Start date:
2017-09-09
Due date:
% Done:
0%
Estimated time:
TYPO3 Version:
9
PHP Version:
Tags:
exceptions
Complexity:
Sprint Focus:
On Location Sprint
Description
As of right now, exceptions are instantiated when thrown.
To throw an exception, you need to repeat a lot of boilerplate code that has nothing to do with the code around.
// Instanciate the class
throw new ContentRenderingException(
// Use sprintf to add arguments to the message
sprintf(
'Registered content object class name "%s" must be an instance of AbstractContentObject, but is not!',
// Pass arguments
$fullyQualifiedClassName
),
// Pass the code
1422564295
);
Instead, this logic could be moved inside exception classes using static methods:
namespace TYPO3\CMS\Frontend\ContentObject\Exception;
use TYPO3\CMS\Core\Error\Exception;
class ContentRenderingException extends Exception
{
const CLASS_NAME_WRONG_PARENT = 'Registered content object class name "%s" must be an instance of AbstractContentObject, but is not!';
/**
* @param string $fullyQualifiedClassName
* @return static
*/
public static function invalidContentObjectClassName($fullyQualifiedClassName)
{
return self::makeNewInstance(
self::CLASS_NAME_WRONG_PARENT,
1422564295,
[$fullyQualifiedClassName]
);
}
}
And inside the exception class:
throw ContentRenderingException::invalidContentObjectClassName($fullyQualifiedClassName);
The root exception class (\TYPO3\CMS\Core\Exception
) will contain a method easing the creation of new instances:
namespace TYPO3\CMS\Core;
class Exception extends \Exception
{
/**
* @param string $message
* @param string $code
* @param array $arguments
* @return static
*/
final protected static function makeNewInstance($message, $code, array $arguments = [])
{
return new static(vsprintf($message, $arguments), $code);
}
}
Actions