Paragraphs Module's Entity\Paragraph->toUrl() May Return String Throwing and Error

Created on 15 March 2023, over 1 year ago
Updated 12 April 2023, over 1 year ago

Problem/Motivation

\Drupal\paragraphs\Entity\Paragraph->toUrl() may return a String. The link extractor service expects a \Drupal\Core\Url instance. It makes later subsequent calls against the returned value, and an \Error is thrown. However, the code is written to handle only \Exceptions.

Steps to reproduce

  1. Install the Paragraphs module.
  2. Install linkchecker.
  3. Add a paragraph type with a rich text field. Enable linkchecker on the rich text field.
  4. Create some content that uses this paragraph.
  5. Save.

Actual

The website encountered an unexpected error. Please try again later.
Error: Call to a member function setAbsolute() on string in Drupal\linkchecker\LinkExtractorService->extractFromField() (line 133 of modules/contrib/linkchecker/src/LinkExtractorService.php).

Excepted

Saves successfully.

Error: Call to a member function setAbsolute() on string in                       
Drupal\linkchecker\LinkExtractorService->extractFromField() (line 133 of          
/var/www/web/modules/contrib/linkchecker/src/LinkExtractorService.php)

Proposed resolution

diff --git a/src/LinkExtractorService.php b/src/LinkExtractorService.php
index ac660ef..fdab9f5 100644
--- a/src/LinkExtractorService.php
+++ b/src/LinkExtractorService.php
@@ -133,7 +133,7 @@ class LinkExtractorService {
           ->setAbsolute()
           ->toString();
       }
-      catch (\Exception $e) {
+      catch (\Throwable $e) {
         $baseContentUrl = NULL;
       }

The code behaves as expected and tolerates a slightly broader range of failures.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States perrygeorget

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • @perrygeorget opened merge request.
  • πŸ‡³πŸ‡΄Norway eiriksm Norway

    Seems like it should be possible to at least write a simple test to reproduce the problem and verify the fix?

  • πŸ‡ΊπŸ‡ΈUnited States perrygeorget

    This is unit testable for sure.

    Sad path:

    1. Have toURL() return a string
    2. Verify that the bad behavior is handled gracefully.

    Happy path:

    1. Have toURL() throws the appropriate exception
    2. Verify that the bad behavior is handled gracefully.
Production build 0.71.5 2024