Skip to content

Commit fcdaf6c

Browse files
committed
feature: use error pages
for #642
1 parent ec1b2e2 commit fcdaf6c

3 files changed

Lines changed: 19 additions & 22 deletions

File tree

src/Middleware/ErrorRequestHandler.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public function handle(
4343
$this->originalUri = $request->getUri();
4444
$errorUri = new Uri("/_$errorCode");
4545
$errorRequest = $request->withUri($errorUri);
46-
47-
$this->completeRequestHandling($errorRequest);
46+
$this->completeRequestHandling($errorRequest, true);
4847
$this->response = $this->response->withStatus($errorCode);
4948
return $this->response;
5049
}

src/Middleware/Lifecycle.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,7 @@ public function start():void {
9797
$response = $this->process($request, $handler);
9898
}
9999
catch(Throwable $throwable) {
100-
echo "<pre>";
101-
echo get_class($throwable), " - ";
102-
$filePath = $throwable->getFile();
103-
$filePath = str_replace(getcwd(), '', $filePath);
104-
echo strip_tags($throwable->getMessage() . " " . $filePath . ":" . $throwable->getLine()), PHP_EOL;
105-
die();
106-
$this->throwable = $throwable;
100+
Log::critical($throwable->getMessage(), ["uri" => $request->getUri()] + $request->getHeaders() + $throwable->getTrace()[0]);
107101

108102
$errorHandler = new ErrorRequestHandler(
109103
ConfigFactory::createForProject(
@@ -119,11 +113,6 @@ public function start():void {
119113
$originalGlobals["server"],
120114
);
121115
$response = $this->process($request, $errorHandler);
122-
123-
trigger_error(
124-
$throwable->getMessage(),
125-
E_USER_ERROR,
126-
);
127116
}
128117

129118
// Now we can finish the HTTP lifecycle by providing the HTTP response for
@@ -206,6 +195,9 @@ public function finish(
206195

207196
foreach($response->getHeaders() as $key => $value) {
208197
$stringValue = implode(", ", $value);
198+
if(strtolower($key) === "location" && str_starts_with($stringValue, "/_")) {
199+
continue;
200+
}
209201
header("$key: $stringValue", true);
210202
}
211203

src/Middleware/RequestHandler.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
use Psr\Http\Message\ResponseInterface;
4949
use Psr\Http\Message\ServerRequestInterface;
5050
use Psr\Http\Server\RequestHandlerInterface;
51+
use Throwable;
5152

5253
class RequestHandler implements RequestHandlerInterface {
5354
/** @var callable(ResponseInterface,ConfigSection) */
@@ -105,7 +106,8 @@ public function handle(
105106
}
106107

107108
protected function completeRequestHandling(
108-
ServerRequestInterface $request
109+
ServerRequestInterface $request,
110+
bool $ignoreLogicErrors = false,
109111
):void {
110112
$this->setupResponse($request);
111113
$this->forceTrailingSlashes($request);
@@ -130,12 +132,19 @@ protected function completeRequestHandling(
130132

131133
$this->handleProtectedGlobals();
132134

133-
if(isset($this->viewModel) && $this->viewModel instanceof HTMLDocument) {
134-
$this->handleHTMLDocumentViewModel();
135+
try {
136+
if(isset($this->viewModel) && $this->viewModel instanceof HTMLDocument) {
137+
$this->handleHTMLDocumentViewModel();
135138
// $this->handleCsrf($request);
136-
}
139+
}
137140

138-
$this->handleLogicExecution($this->logicAssembly);
141+
$this->handleLogicExecution($this->logicAssembly);
142+
}
143+
catch(Throwable $throwable) {
144+
if(!$ignoreLogicErrors) {
145+
throw $throwable;
146+
}
147+
}
139148

140149
// TODO: Why is this in the handle function?
141150
$documentBinder = $this->serviceContainer->get(Binder::class);
@@ -277,9 +286,6 @@ protected function handleHTMLDocumentViewModel():void {
277286

278287
foreach($expandedLogicAssemblyList as $i => $assembly) {
279288
$componentElement = $expandedComponentList[$i];
280-
if(!$componentElement) {
281-
var_dump($assembly);die();
282-
}
283289
$this->handleLogicExecution($assembly, $componentElement);
284290
}
285291
}

0 commit comments

Comments
 (0)