Index: src/Plugin/Validation/Constraint/TweetVisibleConstraintValidator.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- src/Plugin/Validation/Constraint/TweetVisibleConstraintValidator.php (revision 66fdcaa2453b5b587a581b15fb57c1b2d836b6eb) +++ src/Plugin/Validation/Constraint/TweetVisibleConstraintValidator.php (date 1554899241000) @@ -3,9 +3,11 @@ namespace Drupal\media_entity_twitter\Plugin\Validation\Constraint; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\media_entity\EmbedCodeValueTrait; use Drupal\media_entity_twitter\Plugin\MediaEntity\Type\Twitter; use GuzzleHttp\Client; +use GuzzleHttp\Exception\ClientException; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; @@ -24,21 +26,35 @@ */ protected $httpClient; + /** + * The logger channel. + * + * @var \Drupal\Core\Logger\LoggerChannelInterface + */ + protected $logger; + /** * Constructs a new TweetVisibleConstraintValidator. * * @param \GuzzleHttp\Client $http_client * The http client service. + * @param \Drupal\Core\Logger\LoggerChannelInterface $logger + * The logger channel. */ - public function __construct(Client $http_client) { + public function __construct(Client $http_client, + LoggerChannelInterface $logger) { $this->httpClient = $http_client; + $this->logger = $logger; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { - return new static($container->get('http_client')); + return new static( + $container->get('http_client'), + $container->get('logger.factory')->get('media_entity_twitter') + ); } /** @@ -64,7 +80,16 @@ } // Fetch content from the given url. - $response = $this->httpClient->get($matches[0][0], ['allow_redirects' => FALSE]); + try { + $response = $this->httpClient->get($matches[0][0], ['allow_redirects' => FALSE]); + } + catch (ClientException $e) { + $this->logger->warning($e->getMessage()); + if ($e->getCode() === 404) { + $this->context->addViolation($constraint->message); + return; + } + } if ($response->getStatusCode() == 302 && ($location = $response->getHeader('location'))) { $effective_url_parts = parse_url($location[0]);