Skip to content

Commit

Permalink
Merge pull request #6269 from kenjis/fix-forInvalidRedirectRoute-message
Browse files Browse the repository at this point in the history
fix: route name is not displayed in Exception message
  • Loading branch information
samsonasik authored Jul 20, 2022
2 parents 9d45712 + f9931e4 commit fe45d33
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 5 additions & 1 deletion system/HTTP/RedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,20 @@ public function to(string $uri, ?int $code = null, string $method = 'auto')
* Sets the URI to redirect to but as a reverse-routed or named route
* instead of a raw URI.
*
* @param string $route Named route or Controller::method
*
* @throws HTTPException
*
* @return $this
*/
public function route(string $route, array $params = [], int $code = 302, string $method = 'auto')
{
$namedRoute = $route;

$route = Services::routes()->reverseRoute($route, ...$params);

if (! $route) {
throw HTTPException::forInvalidRedirectRoute($route);
throw HTTPException::forInvalidRedirectRoute($namedRoute);
}

return $this->redirect(site_url($route), $method, $code);
Expand Down
2 changes: 1 addition & 1 deletion system/Language/en/HTTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
'emptySupportedNegotiations' => 'You must provide an array of supported values to all Negotiations.',

// RedirectResponse
'invalidRoute' => '{0} route cannot be found while reverse-routing.',
'invalidRoute' => 'The route for "{0}" cannot be found.',

// DownloadResponse
'cannotSetBinary' => 'When setting filepath cannot set binary.',
Expand Down
15 changes: 14 additions & 1 deletion tests/system/HTTP/RedirectResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ public function testRedirectRoute()
$this->assertSame('http://example.com/index.php/exampleRoute', $response->getHeaderLine('Location'));
}

public function testRedirectRouteBad()
public function testRedirectRouteBadNamedRoute()
{
$this->expectException(HTTPException::class);
$this->expectExceptionMessage('The route for "differentRoute" cannot be found.');

$response = new RedirectResponse(new App());

Expand All @@ -92,6 +93,18 @@ public function testRedirectRouteBad()
$response->route('differentRoute');
}

public function testRedirectRouteBadControllerMethod()
{
$this->expectException(HTTPException::class);
$this->expectExceptionMessage('The route for "Bad::badMethod" cannot be found.');

$response = new RedirectResponse(new App());

$this->routes->add('exampleRoute', 'Home::index');

$response->route('Bad::badMethod');
}

public function testRedirectRelativeConvertsToFullURI()
{
$response = new RedirectResponse($this->config);
Expand Down

0 comments on commit fe45d33

Please sign in to comment.