Skip to content

Commit b79566a

Browse files
author
Greg Bowler
authored
Error handling (#586)
* feature: debug output for #447 * wip: the main error flow is defined for #299 * tweak: reverse autoload location search * wip: error handling * wip: error handling functionality for #299 * feature: categorise buffer output closes #585 * feature: debug output and catch errors closes #447 * feature: errors caught and passed to appropriate error pages closes #299
1 parent 8bb7416 commit b79566a

File tree

8 files changed

+510
-173
lines changed

8 files changed

+510
-173
lines changed

build.default.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@
77
},
88
"execute": {
99
"command": "webpack",
10-
"arguments": ["--entry","./script/script.es6", "--output-path", "./www", "--output-filename", "script.js", "--devtool", "source-map", "--mode", "production"]
10+
"arguments": ["--entry","./script/script.es6", "--output-path", "./www", "--output-filename", "script.js", "--devtool", "source-map", "--mode", "development"]
11+
}
12+
},
13+
14+
"script/*sw.js": {
15+
"require": {
16+
"vendor/bin/sync": "*"
17+
},
18+
"execute": {
19+
"command": "vendor/bin/sync",
20+
"arguments": ["--pattern", "*sw.js", "script", "www/"]
1121
}
1222
},
1323

composer.lock

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
chdir(dirname($_SERVER["DOCUMENT_ROOT"]));
1010
ini_set("display_errors", "on");
11+
ini_set("html_errors", "false");
1112
/**
1213
* Before any code is executed, return false here if a static file is requested.
1314
* When running the PHP inbuilt server, this will output the static file.
@@ -26,7 +27,7 @@
2627
* files exist.
2728
* @link https://getcomposer.org/doc/00-intro.md
2829
*/
29-
foreach([__DIR__, dirname($_SERVER["DOCUMENT_ROOT"])] as $dir) {
30+
foreach([dirname($_SERVER["DOCUMENT_ROOT"]), __DIR__] as $dir) {
3031
$autoloadPath = "$dir/vendor/autoload.php";
3132
if(file_exists($autoloadPath)) {
3233
/** @noinspection PhpIncludeInspection */

src/Logic/LogicExecutor.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Gt\WebEngine\Logic;
33

4+
use Generator;
45
use Gt\Routing\Assembly;
56
use Gt\Routing\LogicStream\LogicStreamNamespace;
67
use Gt\Routing\LogicStream\LogicStreamWrapper;
@@ -17,7 +18,8 @@ public function __construct(
1718
}
1819
}
1920

20-
public function invoke(string $name):void {
21+
/** @return Generator<string> filename::function() */
22+
public function invoke(string $name):Generator {
2123
foreach($this->assembly as $file) {
2224
$nsProject = (string)(new LogicProjectNamespace(
2325
$file,
@@ -36,6 +38,7 @@ public function invoke(string $name):void {
3638
$instance,
3739
$name
3840
);
41+
yield "$file::$name()";
3942
}
4043
}
4144
else {
@@ -52,6 +55,7 @@ public function invoke(string $name):void {
5255
null,
5356
$fnReference
5457
);
58+
yield "$file::$name()";
5559
}
5660
}
5761
}
@@ -60,6 +64,6 @@ public function invoke(string $name):void {
6064

6165
private function loadLogicFile(string $file):void {
6266
$streamPath = LogicStreamWrapper::STREAM_NAME . "://$file";
63-
require($streamPath);
67+
require_once($streamPath);
6468
}
6569
}

src/Middleware/DefaultServiceLoader.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Gt\DomTemplate\PlaceholderBinder;
1515
use Gt\DomTemplate\TableBinder;
1616
use Gt\DomTemplate\TemplateCollection;
17+
use Gt\Http\Header\ResponseHeaders;
1718
use Gt\Http\Request;
19+
use Gt\Http\Response;
1820
use Gt\Http\Uri;
1921
use Gt\ServiceContainer\Container;
2022
use Gt\ServiceContainer\LazyLoad;
@@ -26,6 +28,12 @@ public function __construct(
2628
) {
2729
}
2830

31+
#[LazyLoad]
32+
public function loadResponseHeaders():ResponseHeaders {
33+
$response = $this->container->get(Response::class);
34+
return $response->headers;
35+
}
36+
2937
#[LazyLoad]
3038
public function loadDatabase():Database {
3139
$dbSettings = new Settings(
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
namespace Gt\WebEngine\Middleware;
3+
4+
use Gt\Config\Config;
5+
use Gt\Dom\HTMLDocument;
6+
use Gt\DomTemplate\DocumentBinder;
7+
use Gt\Http\ResponseStatusException\ClientError\ClientErrorException;
8+
use Gt\Http\ResponseStatusException\ResponseStatusException;
9+
use Gt\Http\Uri;
10+
use Gt\ServiceContainer\Container;
11+
use Psr\Http\Message\ResponseInterface;
12+
use Psr\Http\Message\ServerRequestInterface;
13+
use Throwable;
14+
15+
class ErrorRequestHandler extends RequestHandler {
16+
public function __construct(
17+
Config $config,
18+
callable $finishCallback,
19+
callable $obCallback,
20+
private Throwable $throwable,
21+
protected Container $serviceContainer,
22+
) {
23+
parent::__construct($config, $finishCallback, $obCallback);
24+
}
25+
26+
public function handle(
27+
ServerRequestInterface $request
28+
):ResponseInterface {
29+
$errorCode = 500;
30+
/** @noinspection PhpConditionAlreadyCheckedInspection */
31+
if($this->throwable instanceof ResponseStatusException
32+
|| $this->throwable instanceof ClientErrorException) {
33+
$errorCode = $this->throwable->getHttpCode();
34+
}
35+
36+
$errorUri = new Uri("/_error/$errorCode/");
37+
$errorRequest = $request->withUri($errorUri);
38+
39+
$this->completeRequestHandling($errorRequest, $this->serviceContainer);
40+
$this->response = $this->response->withStatus($errorCode);
41+
return $this->response;
42+
}
43+
}

0 commit comments

Comments
 (0)