Problem/Motivation

There are several places in the oEmbed API that handle errors that may occur during the process of fetching or otherwise interacting with oEmbed resources, and these errors are either not logged at all, or logged in very general terms that will not help with troubleshooting. We should revisit these areas and improve the error handling.

This issue is spun off from #2831944-237: Implement media source plugin for remote video via oEmbed, point #1.

Steps to reproduce

OEmbed errors occur in certain circumstances, such as when a use created a remote video media entity referring to a video which is restricted or otherwise not publicly accessible, or when the host of the video is not accessible.

Proposed resolution

- improve error messages logged so that the details of the failure can be better investigated
- improve feedback surfaced to the user (suggested in #28 here)

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#55 2023-06-16 15_02_48-Recent log messages _ Drupal.png55.34 KBGrevil
#52 2023-05-26 13_53_44-Recent log messages _ Drush Site-Install.png7.53 KBGrevil
#39 2972846-interdiff-25-39.txt2.43 KBxurizaemon
#39 2972846-39-media-oembed-error.patch5.68 KBxurizaemon
#37 2972846-interdiff-25-37.txt1.97 KBxurizaemon
#37 2972846-37-media-oembed-error.patch5.22 KBxurizaemon
#36 2972846-interdiff-25-35.txt1.31 KBxurizaemon
#35 2972846-interdiff-25-35.txt1.31 KBxurizaemon
#35 2972846-35-media-oembed-error.patch4.38 KBxurizaemon
#33 2972846-interdiff-25-33.txt1.31 KBxurizaemon
#33 2972846-33-media-oembed-error.patch4.38 KBxurizaemon
#30 2972846-30.patch4.35 KBPrem Suthar
#29 reroll_diff_25-29.txt4.29 KBpooja saraah
#29 2972846-29.patch4.44 KBpooja saraah
#25 drupal-9.4.x-core-media-oembed-2972846-25.patch3.75 KBjweowu
#20 2972846-20.patch3.74 KBranjith_kumar_k_u
#15 2972846-12-15-interdiff.txt717 bytesRoSk0
#15 2972846-15-9.1.x.patch3.72 KBRoSk0
#15 2972846-15.patch3.73 KBRoSk0
#14 2972846-12-9.1.x.patch3.74 KBRoSk0
#12 2972846-11-12-interdiff.txt729 bytesRoSk0
#12 2972846-12.patch3.74 KBRoSk0
#11 2972846-11.patch3.03 KBRoSk0

Issue fork drupal-2972846

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

phenaproxima created an issue. See original summary.

phenaproxima’s picture

Title: [PP-1] Improve oEmbed exception logging » Improve oEmbed exception logging
Status: Postponed » Active

The main issue is in.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

rogerpfaff’s picture

Does this include not completely shut down the site if network connection is bad or the oEmbed service (url) is not available? I see no reason to break stuff instead of returning an error informing about that situation.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

phenaproxima’s picture

Issue tags: +oembed

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

phenaproxima’s picture

Category: Task » Feature request
Issue tags: +Triaged Media Initiative issue, +DX (Developer Experience), +WSOD

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

RoSk0’s picture

Status: Active » Needs review
FileSize
3.03 KB

First draft.

Feedback welcome.

RoSk0’s picture

Found another place for improvement. See interdiff.

RoSk0’s picture

To be clear about addition: without it in the locked environment where Drupal talks to the world via proxy and resource destination domain is not allowed you will see this in log for media "cURL error 56: Received HTTP code 403 from proxy after CONNECT (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)". Not helpful at all, but with the request URI you will be clear about what was the request for.

RoSk0’s picture

Re-roll of #12 for 9.1.x.

RoSk0’s picture

As correctly pointed out by the colleague ( thanks Phil ) TransferException doesn't have the getRequest() method.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

DamienMcKenna’s picture

One problem is that Guzzle only returns the first 120 characters of the response if there's an exception in the request; RequestException::create() calls get_message_body_summary() (without the second argument to set the message length so it defaults to 120 characters), which calls Message::bodySummary(), which truncates the string to the default of 120 characters. Argh.

DamienMcKenna’s picture

I think in order to get the full string from guzzle you'd need to extend GuzzleHttp\Exception\RequestException, override the getResponseBodySummary() method and pass in a larger number to get_message_body_summary(), and change core to use the new exception handler instead of the Guzzle one.

ranjith_kumar_k_u’s picture

Re-rolled for 9.4.

Status: Needs review » Needs work

The last submitted patch, 20: 2972846-20.patch, failed testing. View results

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jweowu’s picture

n.b. #20 no longer applies as of 9.4.8.

Martijn de Wit’s picture

I think we need a patch for 10.1.x-dev. That's the new patch version. see #23

jweowu’s picture

The same patch has passed testing for 10.1.x, so I guess we don't need a re-roll for that after all.

I'm still trying to get the testbot to show green for some test cases, but I think those issues are unrelated to the patch.

Kristen Pol’s picture

Can we also get another error message fixed with this patch?

With this video https://www.youtube.com/watch?v=HmZKgaHa3Fg, I was getting this error:

Could not retrieve the oEmbed resource.

which doesn't have the exception message so isn't very helpful.

This should be changed from:

    } catch (TransferException $e) {
      throw new ResourceException('Could not retrieve the oEmbed resource.', $url, [], $e);
    }

to something like:

    catch (TransferException $e) {
      throw new ResourceException('Could not retrieve the oEmbed resource: ' . $e->getMessage(), $url, [], $e);
    }

With the change, the error for this video is:

Could not retrieve the oEmbed resource: Client error: `GET https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=HmZKgaHa3Fg` resulted in a `401 Unauthorized` response: Unauthorized
pooja saraah’s picture

Addressed the comment on #28
Attached patch against Drupal 10.1.x

Prem Suthar’s picture

Try To Fix the #29 Patch Faild To Apply.

Anybody’s picture

Great point @Kristen Pol in #28.
I just came here to write the same. The current message "Could not retrieve the oEmbed resource" is very unclear and you have no good way to identify which oEmbed resource / entity causes the issue!

Just wrote a blog post about that: https://julian.pustkuchen.com/en/understanding-drupal-media-error-messag...

Anybody’s picture

xurizaemon’s picture

#29 didn't apply, and #30 failed tests, so here's #25 reroll + #28 for #drupalsouth code sprint

Status: Needs review » Needs work

The last submitted patch, 33: 2972846-33-media-oembed-error.patch, failed testing. View results

xurizaemon’s picture

Status: Needs work » Needs review
FileSize
4.38 KB
1.31 KB

And an update for the change in error message.

xurizaemon’s picture

FileSize
1.31 KB

Correcting interdiff.

xurizaemon’s picture

Status: Needs review » Needs work

The last submitted patch, 37: 2972846-37-media-oembed-error.patch, failed testing. View results

xurizaemon’s picture

Ok, this works, but on reflection: by changing the error message the user sees, I'm now concerned that we might be straying into messing with localisation.

The text change here is from "Could not retrieve the oEmbed resource." to "Could not retrieve the oEmbed resource: " followed by the supplied exception message.

I don't see instructions on considerations for changing UI text in https://www.drupal.org/docs/develop/user-interface-standards/interface-text - I recall we have responsibilities here though.

On mobile now and can't load localize. Drupal.org but appreciate input on how to best make that improvement.

smustgrave’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: -WSOD +Needs Review Queue Initiative, +Needs issue summary update, +Needs tests

Can the issue summary be updated please. started with the template.

Also will need additional test coverage.

xurizaemon’s picture

Issue summary: View changes

Updating IS from core template.

I am open to NOT changing user facing messages if that permits us to land the original proposed change (logging but not messages) more easily. I'll add an alternative patch without the message and related test change so we have that option.

Given #3202896: Don't display OEmbed error to anonymous visitors when resource stops being available it may be better to leave user facing changes to that issue.

@smustgrave if we go that way, do you feel additional tests are required?

smustgrave’s picture

Would need a test for the logs.

The current test update is just checking for a ":" being added.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Anybody’s picture

Re #41 I agree the user facing message might stay as-is, simply log the details. I think that's what you'd expect and is most safe.

Really annoying to not see details in the log and only this not very helpful message. Perhaps we should gain the priority here.

thomas.frobieter’s picture

Patch #39 applies on 9.5.x but results in the following error when creating a new external Video Media Entity:

Error: Call to undefined method GuzzleHttp\Exception\ClientException::getUrl() in Drupal\media\Plugin\Validation\Constraint\OEmbedResourceConstraintValidator->handleException() (line 142 of core/modules/media/src/Plugin/Validation/Constraint/OEmbedResourceConstraintValidator.php).

Drupal\media\Plugin\Validation\Constraint\OEmbedResourceConstraintValidator->handleException(Object, 'The provided URL does not represent a valid oEmbed resource.') (Line: 120)
Drupal\media\Plugin\Validation\Constraint\OEmbedResourceConstraintValidator->validate(Object, Object) (Line: 201)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateConstraints(Object, '0000000000000a9f0000000000000000', Array) (Line: 153)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode(Object) (Line: 163)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode(Object, Array, 1) (Line: 105)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validate(Object, NULL, NULL) (Line: 93)
Drupal\Core\TypedData\Validation\RecursiveValidator->validate(Object) (Line: 132)
Drupal\Core\TypedData\TypedData->validate() (Line: 489)
Drupal\Core\Entity\ContentEntityBase->validate() (Line: 471)
Drupal\media\Entity\Media->validate() (Line: 188)
Drupal\Core\Entity\ContentEntityForm->validateForm(Array, Object)
call_user_func_array(Array, Array) (Line: 82)
Drupal\Core\Form\FormValidator->executeValidateHandlers(Array, Object) (Line: 275)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object, 'media_remote_video_add_form') (Line: 118)
Drupal\Core\Form\FormValidator->validateForm('media_remote_video_add_form', Array, Object) (Line: 593)
Drupal\Core\Form\FormBuilder->processForm('media_remote_video_add_form', Array, Object) (Line: 325)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 580)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 169)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 153)
Drupal\cryptolog\CryptologMiddleware->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 718)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Edit: The reason is that both kind of Exceptions are handled through the same function:

// Ensure that the URL matches a provider.
    try {
      $provider = $this->urlResolver->getProviderByUrl($url);
    }
    catch (ResourceException $e) {
      $this->handleException($e, $constraint->unknownProviderMessage);
      return;
    }
    catch (ProviderException $e) {
      $this->handleException($e, $constraint->providerErrorMessage);
      return;
    }

but only Drupal\media\OEmbed\ResourceException has a ->getUrl() so that needs to be split.
The second exception here is a GuzzleHttp\Exception\ClientException so the call fails!

Anybody’s picture

Needs work as of #45. One way might be to implement __toString() differently in the two exception classes and just call that instead of ->getUrl() and others.

Another alternative would be to have two different handleException() function. Which way should we go?

Grevil’s picture

There seem to be further Exceptions like "ClientException", which do not have the url part implemented. I guess data would be enough for those exceptions, and we only add the url for when it is an ResourceException?

EDIT: Damn "ClientException" doesn't even have the "getData()" function implemented...

Anybody’s picture

Re #47: Yes!

Grevil’s picture

I just wondered why "ClientException" is even handled by our "handleException" method, since we only catch "ResourceException" and "ProviderException".
The problem is line 153 -> 154 in "/var/www/html/web/core/modules/media/src/Plugin/Validation/Constraint/OEmbedResourceConstraintValidator.php":

[...]
   $this->logger->error($message, $context);
   $e = $e->getPrevious();
} while ($e);

This while loop will rerun our handleException method, with the previous Throwable, and since in my case this is a "ClientException" it will fail on $e->getData().

Is this intended behavior? Should we also handle other exceptions then "ResourceException" and "ProviderException", even if we do not explicitly catch them in code?? Seems pretty dirty to me.

Anybody’s picture

Yes, that's indeed intended, as the exceptions are wrapping each other. I personally don't really like this do ... while thing, but that's not our decision, I guess and existed before this patch.

Anyway special methods may only be called, if they are implemented. As written above, another option would be to override __toString in the Exception to provide further information, instead of using if-clauses, but there are many pro's and con's. Someone has to decide here, which way to go or simply take one way...

Grevil’s picture

Status: Needs work » Needs review
FileSize
7.53 KB

Interdiff between "2972846-39-media-oembed-error.patch" and "2972846-improve-oembed-exception" can be seen here: https://git.drupalcode.org/project/drupal/-/merge_requests/4059/diffs?co....

A minor problem still remains. As we are logging inside a while loop, there is the possibility of logging multiple errors with practically the same output:
screenshot

First log message:

Client error: `GET https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=GtL1h...` resulted in a `400 Bad Request` response: Bad Request

Second log message:

Could not retrieve the oEmbed resource: Client error: `GET https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=GtL1h...` resulted in a `400 Bad Request` response: Bad Request URL: https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=GtL1h....

I also tried adjusting / adding tests, but the currently implemented oembed tests are super "stuffed" and overwhelming. Maybe someone else with a bit more inside knowledge could adjust them accordingly. I would've simply enabled the "media" module and tested on the "remote_video" media type, but that seems way too much unnecessary overhead for such a simple test.

smustgrave’s picture

Status: Needs review » Needs work

Moving to NW for the additional test coverage.

Since this is testing log messages there may be some trait that could do that? Worth posting in #testing for any suggestions.

Grevil’s picture

Status: Needs work » Needs review

Alright, I added an error logging test taking inspiration from dblog/tests/src/Functional/DbLogTest.php, testLogEventPage(), on how to receive the logged watchdog event id. The only problem left is, that the test will fail.
For some reason the latest watchdog log, is NOT our logged oembed error event and the event is NOT logged at all, when checking the watchdog overview!
I am unsure, why that is. I already tried enabling the media and media_library modules, and trying some other workarounds (like pressing the "Add" button multiple times and checking on the dblog overview, for any error event to get logged), but with no avail.

I'll ask in the Slack testing channel. Maybe someone there might have an idea.

Grevil’s picture

This is the test output of the "Recent log messages" page with and without media and media_library enabled, AFTER the "ResourceException" gets thrown and displayed on the page:
screenshot

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: -Needs issue summary update, -Needs tests

Seems test failure is legit to the issue.