Skip to content

duplicateRequest method in ErrorListener should override previous attributes #45102

Closed
@timching

Description

@timching

Description

Not sure this should be a bug or feature request
The duplicateRequest function in ErrorListener will state the _controller, exception & logger

/**
* Clones the request for the exception.
*/
protected function duplicateRequest(\Throwable $exception, Request $request): Request
{
$attributes = [
'_controller' => $this->controller,
'exception' => $exception,
'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
];
$request = $request->duplicate(null, null, $attributes);
$request->setMethod('GET');
return $request;
}

But we may need to use some of the params from the request, is it better that we merge the original attribute instead of replacing it?

(FYI, my working version is 5.3.9)

Example

I am using Sylius, and they are getting the _locale attribute to identify the locale of a page
https://github.com/Sylius/Sylius/blob/75b3fc74454ed04a6d1d9dabea41d401aea8495d/src/Sylius/Bundle/LocaleBundle/Context/RequestBasedLocaleContext.php#L44-L47

currently, if attributes in the main request were not cloning to the error sub-request, links in the error page will be generated incorrectly due to _locale attribute is missing

public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH)
{
$route = null;
$locale = $parameters['_locale']
?? $this->context->getParameter('_locale')
?: $this->defaultLocale;
if (null !== $locale) {
do {
if (null !== ($route = $this->routes->get($name.'.'.$locale)) && $route->getDefault('_canonical_route') === $name) {
break;
}
} while (false !== $locale = strstr($locale, '_', true));
}
if (null === $route = $route ?? $this->routes->get($name)) {
throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions