Skip to content

Commit 4c5ac45

Browse files
committed
better exception message
1 parent 6013536 commit 4c5ac45

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/Web/Router.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,15 @@ public function setDefaults(array $defaults)
116116
*/
117117
public function dispatch($uri = null)
118118
{
119+
$httpMethod = $this->getRequestMethod();
120+
if (empty($uri)) {
121+
$uri = $this->getRewriteUri();
122+
}
123+
119124
$this->handleDispatcherResponse(
120-
$this->createDispatcher()->dispatch(
121-
$this->getRequestMethod(),
122-
$uri ?: $this->getRewriteUri()
123-
)
125+
$this->createDispatcher()->dispatch($httpMethod, $uri),
126+
$httpMethod,
127+
$uri
124128
);
125129
}
126130

@@ -146,16 +150,18 @@ protected function createDispatcher()
146150
* Handle the response from the FastRoute dispatcher.
147151
*
148152
* @param array $routeInfo
153+
* @param string $httpMethod
154+
* @param string $uri
149155
* @throws Exception
150156
*/
151-
protected function handleDispatcherResponse($routeInfo)
157+
protected function handleDispatcherResponse($routeInfo, $httpMethod, $uri)
152158
{
153159
if ($routeInfo[0] == \FastRoute\Dispatcher::NOT_FOUND) {
154-
throw new Exception('Not found handler');
160+
throw new Exception("Not found handler: $uri", 404);
155161
}
156162
if ($routeInfo[0] == \FastRoute\Dispatcher::METHOD_NOT_ALLOWED) {
157163
$allowed = (array)$routeInfo[1];
158-
throw new Exception('Method Not Allowed, allowed: ' . implode(',', $allowed));
164+
throw new Exception("Method Not Allowed: $uri, allowed: " . implode(',', $allowed), 405);
159165
}
160166

161167
// \FastRoute\Dispatcher::FOUND

tests/Web/RouterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function testNotFoundException()
104104

105105
/**
106106
* @expectedException \Exception
107-
* @expectedExceptionMessageRegExp /Method Not Allowed, allowed:.+/
107+
* @expectedExceptionMessage Method Not Allowed
108108
*/
109109
public function testMethodNotAllowedException()
110110
{

0 commit comments

Comments
 (0)