Task #82420
closedIntroduce a standard way of throwing exceptions
0%
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);
}
}
Updated by Gerrit Code Review about 7 years ago
- Status changed from New to Under Review
Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/54076
Updated by Gerrit Code Review about 7 years ago
Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/54076
Updated by Gerrit Code Review about 7 years ago
Patch set 3 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/54076
Updated by Gerrit Code Review about 7 years ago
Patch set 4 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/54076
Updated by Gerrit Code Review about 7 years ago
Patch set 5 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/54076
Updated by Gerrit Code Review about 7 years ago
Patch set 6 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/54076
Updated by Nathan Boiron about 7 years ago
- Status changed from Under Review to Rejected