Project

General

Profile

Actions

Task #82420

closed

Introduce a standard way of throwing exceptions

Added by Nathan Boiron over 6 years ago. Updated over 6 years ago.

Status:
Rejected
Priority:
Could have
Assignee:
Category:
-
Target version:
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 #1

Updated by Gerrit Code Review over 6 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

Actions #2

Updated by Gerrit Code Review over 6 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

Actions #3

Updated by Gerrit Code Review over 6 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

Actions #4

Updated by Gerrit Code Review over 6 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

Actions #5

Updated by Gerrit Code Review over 6 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

Actions #6

Updated by Gerrit Code Review over 6 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

Actions #7

Updated by Nathan Boiron over 6 years ago

  • Status changed from Under Review to Rejected
Actions

Also available in: Atom PDF