This method can be used to convert exceptions inside of methods like __toString()
to PHP errors because exceptions cannot be thrown inside of them.
public static convertExceptionToError ( |
||
$exception | the exception to convert to a PHP error. |
/** * PHP magic method that returns the string representation of this object. * @return string the string representation of this object. */ public function __toString() { // __toString cannot throw exception // use trigger_error to bypass this limitation try { return $this->toString(); } catch (\Exception $e) { ErrorHandler::convertExceptionToError($e); return ''; } }
/** * PHP magic method that returns the string representation of this object. * @see [[$toStringFormat]] * @see [[$toStringScheme]] * @return string the url of this file. */ public function __toString() { // __toString cannot throw exception // use trigger_error to bypass this limitation try { if ($this->status === self::STATUS_INITIALIZED_FILE) { return $this->getUrl($this->toStringFormat, $this->toStringScheme); } else { // when validator is generating errors for just uploaded file // it adds file object to translation params, // but the file cannot get url for just uploaded file $name = $this->getName(); return $name === null ? '' : $name; } } catch (\Exception $e) { ErrorHandler::convertExceptionToError($e); return ''; } }
public function __toString() { $this->addField(); // __toString cannot throw exception // use trigger_error to bypass this limitation try { return $this->render(); } catch (\Exception $e) { \yii\base\ErrorHandler::convertExceptionToError($e); return ''; } }