Skip to content

Commit f68a1f6

Browse files
author
Greg Bowler
committed
wip: error handling
1 parent 74130b9 commit f68a1f6

File tree

6 files changed

+387
-238
lines changed

6 files changed

+387
-238
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: 44 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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\ResponseStatusException;
8+
use Gt\Http\Uri;
9+
use Gt\ServiceContainer\Container;
10+
use Psr\Http\Message\ResponseInterface;
11+
use Psr\Http\Message\ServerRequestInterface;
12+
use Throwable;
13+
14+
class ErrorRequestHandler extends RequestHandler {
15+
public function __construct(
16+
Config $config,
17+
callable $finishCallback,
18+
private Throwable $throwable,
19+
protected Container $serviceContainer,
20+
) {
21+
parent::__construct($config, $finishCallback);
22+
}
23+
24+
public function handle(
25+
ServerRequestInterface $request
26+
):ResponseInterface {
27+
$errorCode = 500;
28+
if($this->throwable instanceof ResponseStatusException) {
29+
$errorCode = $this->throwable->getHttpCode();
30+
}
31+
32+
$errorUri = new Uri("/_$errorCode");
33+
$errorRequest = $request->withUri($errorUri);
34+
35+
$this->completeRequestHandling($errorRequest);
36+
$this->response = $this->response->withStatus($errorCode);
37+
return $this->response;
38+
}
39+
}

src/Middleware/Lifecycle.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ class Lifecycle implements MiddlewareInterface {
4545
private Throwable $throwable;
4646

4747
public function start():void {
48-
set_error_handler($this->error(...), E_ALL);
49-
50-
// set_error_handler(function($errno, $errstr, $errfile, $errline) {
51-
// throw new \Exception($errstr, $errno, 0, $errfile, $errline);
52-
// }, E_WARNING);
5348
// The first thing that's done within the WebEngine lifecycle is start a timer.
5449
// This timer is only used again at the end of the call, when finish() is
5550
// called - at which point the entire duration of the request is logged out (and
@@ -88,16 +83,19 @@ public function start():void {
8883
$response = $this->process($request, $handler);
8984
}
9085
catch(Throwable $throwable) {
91-
// TODO: $response = $this->process($request, $errorHandler)
92-
// There should be some sort of magical error handler created at this point,
93-
// but most of the refactoring of the RequestHandler::handle() function can
94-
// be shared to this other Handler. This kills two birds with one stone, as
95-
// when generating the new response, it should still have user-code executing
96-
// wherever possible. Question: should _common still fire? I don't think so...
97-
/// ... but _error should!
98-
$response = $this->responseFromThrowable($throwable);
99-
10086
$this->throwable = $throwable;
87+
88+
$errorHandler = new ErrorRequestHandler(
89+
ConfigFactory::createForProject(
90+
getcwd(),
91+
"vendor/phpgt/webengine/config.default.ini"
92+
),
93+
$this->finish(...),
94+
$throwable,
95+
$handler->getServiceContainer(),
96+
);
97+
$response = $this->process($request, $errorHandler);
98+
10199
trigger_error(
102100
$throwable->getMessage(),
103101
E_USER_ERROR,
@@ -150,7 +148,7 @@ public function debugOutput(
150148
if(!is_null($detailName)) {
151149
$detailJs .= "console.group(\"$detailName\");";
152150
}
153-
// $detailJs .= "console.log(`" . print_r($detail, true) . "`)";
151+
$detailJs .= "console.log(`" . print_r($detail, true) . "`)";
154152
if(!is_null($detailName)) {
155153
$detailJs .= "console.groupEnd();";
156154
}

0 commit comments

Comments
 (0)