Last active
February 16, 2024 06:59
-
-
Save u-mulder/2e55a41a46b82631d8e7d82c26c49d61 to your computer and use it in GitHub Desktop.
Interrupt execution of bitrix component based on OO approach (using class.php)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use \Bitrix\Main\Error; | |
| use \Bitrix\Main\ErrorCollection; | |
| class MySuperComponent extends CBitrixComponent | |
| { | |
| const SEVERE_ERROR = 1; | |
| const ALARM_ERROR = 2; | |
| /** @var ErrorCollection */ | |
| protected $errorCollection; | |
| public function onPrepareComponentParams($params) | |
| { | |
| // Checking params | |
| $something_is_wrong = true; | |
| if ($something_is_wrong) { | |
| $this->errorCollection->setError( | |
| new Error( | |
| 'SOMETHING_IS_WRONG', | |
| // Add ERROR_CODE so as to set error type and process error of this type as you wish | |
| self::SEVERE_ERROR | |
| ) | |
| ); | |
| // Use `return` if error is serious and it is useless to continue checking | |
| return $params; | |
| } | |
| } | |
| public function executeComponent() | |
| { | |
| if (count($this->errorCollection)) { | |
| /** @var Error $error */ | |
| foreach ($this->errorCollection as $error) { | |
| $code = $error->getCode(); | |
| if ($code === self::SEVERE_ERROR) { | |
| // Show 404 for example | |
| } elseif ($code === self::ALARM_ERROR) { | |
| ShowError($error->getMessage()); | |
| } | |
| } | |
| // Stop component execution | |
| return; | |
| } | |
| // Continue component execution | |
| //... | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment