Skip to content

Instantly share code, notes, and snippets.

@u-mulder
Last active February 16, 2024 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save u-mulder/2e55a41a46b82631d8e7d82c26c49d61 to your computer and use it in GitHub Desktop.
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)
<?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