Skip to content

Commit 5484651

Browse files
committed
big refactor
1 parent ea37a9e commit 5484651

File tree

1 file changed

+65
-62
lines changed

1 file changed

+65
-62
lines changed

resources/lib/UnityHTTPD.php

Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,48 @@ public static function errorID(?\Throwable $e = null): string
5151
return md5(strval(session_id()) . spl_object_id($e));
5252
}
5353

54-
public static function logThrowableAndMessageUser(
55-
\Throwable $error,
54+
public static function gracefulDie(
5655
string $log_title,
5756
string $log_message,
5857
string $user_message_title,
5958
string $user_message_body,
60-
) {
59+
?\Throwable $error = null,
60+
int $http_response_code = 200,
61+
mixed $data = null,
62+
): never {
6163
$errorid = self::errorID($error);
62-
$data = [];
63-
self::errorLog($log_title, $log_message, error: $error, errorid: $errorid, data: $data);
64+
$suffix = sprintf(
65+
"Please notify a Unity admin at %s. Error ID: %s.",
66+
CONFIG["mail"]["support"],
67+
$errorid,
68+
);
6469
if (strlen($user_message_body) == 0) {
65-
$user_message_body = "error ID: $errorid";
70+
$user_message_body = $suffix;
71+
} else {
72+
$user_message_body .= " $suffix";
73+
}
74+
self::errorLog($log_title, $log_message, data: $data, error: $error, errorid: $errorid);
75+
// if the user was doing HTTP POST, then make a pretty error and redirect
76+
// else, a redirect may cause an infinite loop, so fall back on the old ugly error
77+
if ($_SERVER["REQUEST_METHOD"] == "POST") {
78+
self::messageError($user_message_title, $user_message_body);
79+
self::redirect();
6680
} else {
67-
$user_message_body .= " error ID: $errorid";
81+
if (CONFIG["site"]["enable_error_to_user"]) {
82+
if (!headers_sent()) {
83+
http_response_code($http_response_code);
84+
}
85+
// text may not be shown in the webpage in an obvious way, so make a popup
86+
self::alert("$user_message_title -- $user_message_body");
87+
echo "<h1>$user_message_title</h1><p>$user_message_body</p>";
88+
if (!is_null($error) && ini_get("display_errors") && ini_get("html_errors")) {
89+
echo "<table>";
90+
echo $error->xdebug_message;
91+
echo "</table>";
92+
}
93+
}
94+
self::die();
6895
}
69-
self::messageError($user_message_title, $user_message_body);
7096
}
7197

7298
// $data must be JSON serializable
@@ -124,62 +150,52 @@ private static function throwableToArray(\Throwable $t): array
124150
return $output;
125151
}
126152

127-
private static function errorToUser(
128-
string $msg,
129-
int $http_response_code,
130-
?string $errorid = null,
131-
): void {
132-
if (!CONFIG["site"]["enable_error_to_user"]) {
133-
return;
134-
}
135-
$notes = "Please notify a Unity admin at " . CONFIG["mail"]["support"] . ".";
136-
if (!is_null($errorid)) {
137-
$notes = $notes . " Error ID: $errorid.";
138-
}
139-
if (!headers_sent()) {
140-
http_response_code($http_response_code);
141-
}
142-
// text may not be shown in the webpage in an obvious way, so make a popup
143-
self::alert("$msg $notes");
144-
echo "<h1>$msg</h1><p>$notes</p>";
145-
}
146-
147153
public static function badRequest(
148-
string $message,
154+
string $log_message,
149155
?\Throwable $error = null,
150156
?array $data = null,
151157
): never {
152-
$errorid = self::errorID($error);
153-
self::errorToUser("Invalid requested action or submitted data.", 400, $errorid);
154-
self::errorLog("bad request", $message, $errorid, $error, $data);
155-
self::die($message);
158+
self::gracefulDie(
159+
"bad request",
160+
$log_message,
161+
"Invalid requested action or submitted data.",
162+
"",
163+
error: $error,
164+
http_response_code: 400,
165+
data: $data,
166+
);
156167
}
157168

158169
public static function forbidden(
159-
string $message,
170+
string $log_message,
160171
?\Throwable $error = null,
161172
?array $data = null,
162173
): never {
163-
$errorid = self::errorID($error);
164-
self::errorToUser("Permission denied.", 403, $errorid);
165-
self::errorLog("forbidden", $message, $errorid, $error, $data);
166-
self::die($message);
174+
self::gracefulDie(
175+
"forbidden",
176+
$log_message,
177+
"Permission denied.",
178+
"",
179+
error: $error,
180+
http_response_code: 403,
181+
data: $data,
182+
);
167183
}
168184

169185
public static function internalServerError(
170-
string $message,
186+
string $log_message,
171187
?\Throwable $error = null,
172188
?array $data = null,
173189
): never {
174-
$errorid = self::errorID($error);
175-
self::errorToUser("An internal server error has occurred.", 500, $errorid);
176-
self::errorLog("internal server error", $message, $errorid, $error, $data);
177-
if (!is_null($error) && ini_get("display_errors") && ini_get("html_errors")) {
178-
echo "<table>";
179-
echo $error->xdebug_message;
180-
echo "</table>";
181-
}
182-
self::die($message);
190+
self::gracefulDie(
191+
"internal server error",
192+
$log_message,
193+
"An internal server error has occurred.",
194+
"",
195+
error: $error,
196+
http_response_code: 500,
197+
data: $data,
198+
);
183199
}
184200

185201
// https://www.php.net/manual/en/function.set-exception-handler.php
@@ -188,20 +204,7 @@ public static function exceptionHandler(\Throwable $e): void
188204
// we disable log_errors before we enable this exception handler to avoid duplicate logging
189205
// if this exception handler itself fails, information will be lost unless we re-enable it
190206
ini_set("log_errors", true);
191-
// if the user was doing HTTP POST, then make a pretty error and redirect
192-
// else, a redirect may cause an infinite loop, so fall back on the old ugly error
193-
if ($_SERVER["REQUEST_METHOD"] == "POST") {
194-
self::logThrowableAndMessageUser(
195-
$e,
196-
"uncaught exception",
197-
strval($e),
198-
"An internal server error has occurred.",
199-
"",
200-
);
201-
self::redirect();
202-
} else {
203-
self::internalServerError("", error: $e);
204-
}
207+
self::internalServerError("", error: $e);
205208
}
206209

207210
public static function errorHandler(int $severity, string $message, string $file, int $line)

0 commit comments

Comments
 (0)