ExceptionHandlerBlock.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Element;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Framework\Phrase;
  9. /**
  10. * Class of a Exception Handler Block
  11. *
  12. * Block for default and maintenance mode. During layout loading process corrupted block (that throws exception)
  13. * will be replaced with a "dummy" block. As result, page will be loaded without broken block.
  14. *
  15. * When calls from parent to child block occurred and the error appeared in the child block,
  16. * all blocks chain would be removed.
  17. */
  18. class ExceptionHandlerBlock implements BlockInterface
  19. {
  20. /**
  21. * @var string
  22. */
  23. protected $blockName;
  24. /**
  25. * @param string $blockName
  26. */
  27. public function __construct($blockName = '')
  28. {
  29. $this->blockName = $blockName;
  30. }
  31. /**
  32. * Throws an exception when parent block calls corrupted child block method
  33. *
  34. * @param string $method
  35. * @param array $args
  36. * @return void
  37. * @throws LocalizedException
  38. */
  39. public function __call($method, $args)
  40. {
  41. throw new LocalizedException(
  42. new Phrase('The "%1" block threw an exception, and it can\'t be rendered.', [$this->blockName])
  43. );
  44. }
  45. /**
  46. * Declared in BlockInterface and also throws an exception
  47. *
  48. * @throws LocalizedException
  49. * @return void
  50. */
  51. public function toHtml()
  52. {
  53. throw new LocalizedException(
  54. new Phrase('The "%1" block threw an exception, and it can\'t be rendered.', [$this->blockName])
  55. );
  56. }
  57. }