ExceptionMessageFactory.php 1002 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Message;
  7. use Magento\Framework\Exception\RuntimeException;
  8. class ExceptionMessageFactory implements ExceptionMessageFactoryInterface
  9. {
  10. /**
  11. * @var \Magento\Framework\Message\Factory
  12. */
  13. private $messageFactory;
  14. /**
  15. * @param Factory $messageFactory
  16. */
  17. public function __construct(Factory $messageFactory)
  18. {
  19. $this->messageFactory = $messageFactory;
  20. }
  21. /**
  22. * @inheritdoc
  23. */
  24. public function createMessage(\Exception $exception, $type = MessageInterface::TYPE_ERROR)
  25. {
  26. if ($exception instanceof \Exception) {
  27. return $this->messageFactory->create($type)
  28. ->setText($exception->getMessage());
  29. }
  30. throw new RuntimeException(
  31. new \Magento\Framework\Phrase("Exception instance doesn't match %1 type", [\Exception::class])
  32. );
  33. }
  34. }