Skip to content

Commit e82e017

Browse files
committed
wip: improve throwable logging and remove unused error handling methods
1 parent ab11c55 commit e82e017

File tree

3 files changed

+47
-33
lines changed

3 files changed

+47
-33
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ The [Github issue tracker][issues] is used to submit bug reports, feature reques
8585

8686
It would be helpful if you could create your issue in the appropriate repository - for instance, if the issue/question is regarding using a database in WebEngine, https://github.com/phpgt/Database/issues would be the best place - but it's fine to create the issue on WebEngine's issue tracker, and someone can then move the issue if necessary.
8787

88-
### Chat to a developer
89-
90-
A hands-on dev chat system is currently being planned
91-
9288
[quick-start]: https://github.com/PhpGt/WebEngine/wiki/Quick-start
9389
[tutorials]: https://github.com/PhpGt/WebEngine/wiki/hello-world-tutorial
9490
[contributing]: https://github.com/PhpGt/WebEngine/blob/master/CONTRIBUTING.md

src/Middleware/DefaultServiceLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class DefaultServiceLoader {
2626
public function __construct(
2727
protected Config $config,
2828
protected Container $container
29-
) { }
29+
) {}
3030

3131
public function loadResponseHeaders():ResponseHeaders {
3232
$response = $this->container->get(Response::class);

src/Middleware/Lifecycle.php

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
use Gt\Config\ConfigFactory;
55
use Gt\Config\ConfigSection;
66
use Gt\Http\RequestFactory;
7-
use Gt\Http\Response;
8-
use Gt\Http\ResponseFactory;
97
use Gt\Http\StatusCode;
10-
use Gt\Http\Stream;
118
use Gt\Logger\Log;
9+
use Gt\Session\Session;
1210
use Gt\WebEngine\Debug\Timer;
1311
use Psr\Http\Message\ResponseInterface;
1412
use Psr\Http\Message\ServerRequestInterface;
@@ -42,7 +40,6 @@
4240
*/
4341
class Lifecycle implements MiddlewareInterface {
4442
private Timer $timer;
45-
private Throwable $throwable;
4643

4744
public function start():void {
4845
// Before we start, we check if the current URI should be redirected. If it
@@ -97,7 +94,50 @@ public function start():void {
9794
$response = $this->process($request, $handler);
9895
}
9996
catch(Throwable $throwable) {
100-
Log::critical($throwable->getMessage(), ["uri" => $request->getUri()] + $request->getHeaders() + $throwable->getTrace()[0]);
97+
$ipSesss = $originalGlobals["server"]["REMOTE_ADDR"] . "#" . substr(session_id(), 0, 4);
98+
$trace = $throwable->getTrace();
99+
$stopTraceAt = null;
100+
foreach($trace as $i => $traceItem) {
101+
if(str_contains($traceItem["file"] ?? "", "vendor/phpgt/")) {
102+
$stopTraceAt = $i;
103+
break;
104+
}
105+
106+
$orderedItem = [];
107+
foreach(["class", "function", "type", "file", "line"] as $key) {
108+
if(isset($traceItem[$key])) {
109+
$orderedItem[$key] = $traceItem[$key];
110+
}
111+
}
112+
$trace[$i] = $orderedItem + $traceItem;
113+
114+
if(str_starts_with($traceItem["file"] ?? "", "gt-logic-stream://")) {
115+
$trace[$i]["file"] = substr($traceItem["file"], strlen("gt-logic-stream://"));
116+
}
117+
if(str_starts_with($traceItem["function"], "Gt\\AppLogic\\")) {
118+
$trace[$i]["function"] = substr($traceItem["function"], strlen("Gt\\AppLogic\\"));
119+
$trace[$i]["function"] = str_replace("_php\\", ".php\\", $trace[$i]["function"]);
120+
}
121+
}
122+
if($stopTraceAt) {
123+
$trace = array_slice($trace, 0, $stopTraceAt);
124+
}
125+
126+
$cwd = getcwd();
127+
$file = $throwable->getFile();
128+
if(str_starts_with($file, $cwd)) {
129+
$file = substr($file, strlen($cwd));
130+
$file = trim($file, "/");
131+
}
132+
133+
Log::critical($throwable->getMessage(), [
134+
"session" => $ipSesss,
135+
"uri" => $request->getUri(),
136+
"file" => $file,
137+
"line" => $throwable->getLine(),
138+
"trace" => $trace,
139+
"body" => file_get_contents("php://input"),
140+
]);
101141

102142
$errorHandler = new ErrorRequestHandler(
103143
ConfigFactory::createForProject(
@@ -112,6 +152,7 @@ public function start():void {
112152
$originalGlobals["files"],
113153
$originalGlobals["server"],
114154
);
155+
115156
$response = $this->process($request, $errorHandler);
116157
}
117158

@@ -135,21 +176,6 @@ public function process(
135176
return $handler->handle($request);
136177
}
137178

138-
public function error(
139-
int $errno,
140-
string $errstr,
141-
?string $errfile = null,
142-
?int $errline = null,
143-
?array $errcontext = null,
144-
):bool {
145-
$params = ["error", $errstr];
146-
if(isset($this->throwable)) {
147-
array_push($params, $this->throwable, get_class($this->throwable));
148-
}
149-
call_user_func_array($this->debugOutput(...), $params);
150-
return true;
151-
}
152-
153179
public function debugOutput(
154180
string $name,
155181
string $message,
@@ -178,14 +204,6 @@ public function debugOutput(
178204
echo $js;
179205
}
180206

181-
public function responseFromThrowable(Throwable $throwable):Response {
182-
$response = new Response();
183-
$body = new Stream();
184-
$body->write("errrrrrrrrrrror!");
185-
$response = $response->withBody($body);
186-
return $response;
187-
}
188-
189207
public function finish(
190208
ResponseInterface $response,
191209
ConfigSection $appConfig

0 commit comments

Comments
 (0)