Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/php/web/frontend/CannotRoute.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php namespace web\frontend;

use web\Error;

class CannotRoute extends Error {
public $method, $path;

/**
* Creates a new *cannot route* error
*
* @param string $method
* @param string $path
* @param lang.Throwable $cause
*/
public function __construct($method, $path, $cause= null) {
parent::__construct(404, "Cannot route {$method} requests to {$path}", $cause);
$this->method= $method;
$this->path= $path;
}
}
2 changes: 1 addition & 1 deletion src/main/php/web/frontend/Frontend.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function view($req, $res, $delegate, $matches= []) {
static $CSRF_EXEMPT= ['get' => true, 'head' => true];

if (null === $delegate) {
return $this->errors()->handle(new Error(404, 'Cannot route '.$req->method().' requests to '.$req->uri()->path()));
return $this->errors()->handle(new CannotRoute($req->method(), $req->uri()->path()));
}

// Verify CSRF token for anything which is not a GET or HEAD request
Expand Down
4 changes: 2 additions & 2 deletions src/test/php/web/frontend/unittest/HandlingTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use test\Assert;
use test\{Expect, Test, TestCase, Values};
use web\frontend\unittest\actions\{Blogs, Home, Select, Users, Posts};
use web\frontend\{Frontend, Templates, View};
use web\frontend\{Frontend, Templates, View, CannotRoute};
use web\io\{TestInput, TestOutput};
use web\{Error, Request, Response};

Expand Down Expand Up @@ -172,7 +172,7 @@ public function write($template, $context, $out) { /* NOOP */ }
Assert::equals(404, $res->status());
}

#[Test, Expect(class: Error::class, message: '/Cannot route PATCH requests to .+/')]
#[Test, Expect(class: CannotRoute::class, message: '/Cannot route PATCH requests to .+/')]
public function unsupported_route() {
$fixture= new Frontend(new Users(), new class() implements Templates {
public function write($template, $context, $out) { /* NOOP */ }
Expand Down
Loading