CurlDispatcher timeout should be caught and rather not crash the page

Created on 8 May 2023, over 1 year ago
Updated 9 May 2023, over 1 year ago

Problem/Motivation

A timeout exception with CurlDispatcher should be caught gracefully and inject some message into the text, rather than crashing the entire page. Here's an example stacktrace:

Embed\Http\NetworkException: Resolving timed out after 10000 milliseconds in Embed\Http\CurlDispatcher->error() (line 158 of vendor/embed/embed/src/Http/CurlDispatcher.php).
Embed\Http\CurlDispatcher->exec() (Line: 32)
Embed\Http\CurlDispatcher::fetch() (Line: 31)
Embed\Http\CurlClient->sendRequest() (Line: 56)
Embed\Http\Crawler->sendRequest() (Line: 24)
Embed\Embed->get() (Line: 65)
Drupal\url_embed\UrlEmbed->getEmbed() (Line: 141)
Drupal\url_embed\Plugin\Filter\ConvertUrlToEmbedFilter::Drupal\url_embed\Plugin\Filter\{closure}()
preg_replace_callback() (Line: 148)
Drupal\url_embed\Plugin\Filter\ConvertUrlToEmbedFilter::convertUrls() (Line: 43)
Drupal\url_embed\Plugin\Filter\ConvertUrlToEmbedFilter->process() (Line: 118)
Drupal\filter\Element\ProcessedText::preRenderText()
call_user_func_array() (Line: 101)

Steps to reproduce

Maybe temporarily block a provider with a firewall to provoke a timeout.

Proposed resolution

Catch exception and inject a message instead of the embedded medium.

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @AndersTwo
  • This particular exception happens when a YouTube video is no longer available or private, which is a very common situation.

    The crash can be temporarily fixed in ConvertUrlToEmbedFilter.php as shown in the snippet below, although it's really only a first attempt at getting past the road block. Please excuse the misplaced use of a Bootstrap alert here. You certainly know a better way to inject a message to make end users aware of a problem with one of the URLs in the text.

     public function process($text, $langcode) {
        try {
          $result = new FilterProcessResult(static::convertUrls($text, $this->settings['url_prefix']));
        }
        catch (NetworkException $e) {
          $result = new FilterProcessResult('<p class="alert alert-danger">A network exception occurred with embedding an URL</p> ' . $text);
        }
        return $result;
      }
Production build 0.71.5 2024