Problem/Motivation
When uploading a file, eg. under /media/add/%media_type, there is an error, which prevents the file from beeing uploaded:
"
An AJAX HTTP error occurred.
HTTP Result Code: 500
Debugging information follows.
Path: /media/add/vector_image?element_parents=field_media_svg/widget/0&ajax_form=1
StatusText: error
ResponseText: The website encountered an unexpected error. Please try again later.UnexpectedValueException: base:/sites/default/files/2024-04/test-svgrepo-com_1.svg has no corresponding route. in Drupal\Core\Url->getRouteName() (line 567 of core/lib/Drupal/Core/Url.php). Drupal\rabbit_hole_links\RabbitHoleLinksLinkGenerator->alterUrl(Object) (Line: 38)
Drupal\rabbit_hole_links\RabbitHoleLinksLinkGenerator->generate('test-svgrepo-com_1.svg', Object) (Line: 95)
Drupal\Core\Render\Element\Link::preRenderLink(Array)
call_user_func_array(Array, Array) (Line: 111)
Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 797)
Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 386)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array) (Line: 474)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 45)
__TwigTemplate_4eea2ecec090580373f8b2d9cab04aa2->doDisplay(Array, Array) (Line: 394)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
Twig\Template->display(Array) (Line: 379)
Twig\Template->render(Array, Array) (Line: 40)
Twig\TemplateWrapper->render(Array) (Line: 53)
twig_render_template('core/themes/claro/templates/field/file-link.html.twig', Array) (Line: 372)
Drupal\Core\Theme\ThemeManager->render('file_link', Array) (Line: 445)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 458)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array) (Line: 474)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 51)
__TwigTemplate_87262786ce9194570ff5c741ec9535dd->doDisplay(Array, Array) (Line: 394)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
Twig\Template->display(Array, Array) (Line: 46)
__TwigTemplate_bd753859084bfd566e11e5d5af5cf297->doDisplay(Array, Array) (Line: 394)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
Twig\Template->display(Array) (Line: 379)
Twig\Template->render(Array, Array) (Line: 40)
Twig\TemplateWrapper->render(Array) (Line: 53)
twig_render_template('core/themes/claro/templates/content-edit/image-widget.html.twig', Array) (Line: 372)
Drupal\Core\Theme\ThemeManager->render('image_widget', Array) (Line: 445)
Drupal\Core\Render\Renderer->doRender(Array, 1) (Line: 204)
Drupal\Core\Render\Renderer->render(Array, 1) (Line: 148)
Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() (Line: 592)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 149)
Drupal\Core\Render\Renderer->renderRoot(Array) (Line: 200)
Drupal\file\Element\ManagedFile::uploadAjaxCallback(Array, Object, Object)
call_user_func_array(Array, Array) (Line: 69)
Drupal\Core\Form\FormAjaxResponseBuilder->buildResponse(Object, Array, Object, Array) (Line: 109)
Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber->onException(Object, 'kernel.exception', Object)
call_user_func(Array, Object, 'kernel.exception', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.exception') (Line: 239)
Symfony\Component\HttpKernel\HttpKernel->handleThrowable(Object, Object, 1) (Line: 91)
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: 53)
Asm89\Stack\Cors->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: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
"
Steps to reproduce
Try to add a file for an arbitrary media_type, check the console for errors.
Proposed resolution
In RabbitHoleLinksLinkGenerator.php, the method alterUrl() tries to get the route name for given $url. When an url is not routed (like eg. file url's), the error will happen. To solve this, it should be ensured that the url is routed, before calling getRouteName(). This can happen via $url->isRouted().