Handle exceptions.

This commit is contained in:
Igor Barkov (iwork) 2020-06-23 15:22:32 +03:00
parent 50f04b3a3c
commit 9ce4a48f42

View File

@ -6,19 +6,18 @@
*/ */
session_start(); session_start();
if ($_SESSION['uid'] == null) { if (empty($_SESSION['uid'])) {
header("Location: index.php"); header("Location: index.php");
} }
include_once "config.php"; require_once "config.php";
//include_once "functions.php"; //include_once "functions.php";
$msg = filter_input(INPUT_POST,'msg'); $msg = filter_input(INPUT_POST, 'msg');
$uid = $_SESSION['uid']; $uid = $_SESSION['uid'];
if ($msg) { if ($msg) {
try { try { db::c()->query('INSERT INTO `chat` (`uid`, `msg`) VALUES (?i, "?s")', $uid, $msg); }
db::c()->query('INSERT INTO `chat` (`uid`, `msg`) VALUES (?i, "?s")', $uid, $msg); catch (\Krugozor\Database\Mysql\Exception $e) {
} catch (\Krugozor\Database\Mysql\Exception $e) { echo "<div style='background-color: #ffaaaa;'>Ошибка: " . $e->getMessage() . "<br> В файле: " . $e->getFile() . " (" . $e->getLine() . ")</div>";
echo "<div class='private'>DB Error: ". $e->getMessage() . "<br> File: " . $e->getFile() . " (".$e->getLine().")</div>";
} }
} }
@ -32,20 +31,23 @@ function show_messages()
(SELECT `login` FROM `users` WHERE `users`.`id` = `uid`) AS `from`, (SELECT `login` FROM `users` WHERE `users`.`id` = `uid`) AS `from`,
`type` `type`
FROM `chat` ORDER BY `id` LIMIT 50'); FROM `chat` ORDER BY `id` LIMIT 50');
} catch (\Krugozor\Database\Mysql\Exception $e) {
echo "<div class='private'>DB Error: ". $e->getMessage() . "<br> File: " . $e->getFile() . " (".$e->getLine().")</div>";
}
while ($message = $chat->fetch_assoc()) { while ($message = $chat->fetch_assoc()) {
$d = new DateTime($message['msgdate']); $d = new DateTime($message['msgdate']);
$m = htmlspecialchars($message['msg']); $m = htmlspecialchars($message['msg']);
if ($message['type'] == 'sys') { /* Системка */ if ($message['type'] == 'sys') { /* Системка */
echo sprintf("<span style='color:maroon;background:#faa;'>%s %s</span>", $d->format('H:i'),$m).PHP_EOL; echo sprintf("<span style='color:maroon;background:#faa;'>%s %s</span>", $d->format('H:i'), $m) . PHP_EOL;
} elseif ($message['type'] == 'sms') { /* Телеграмма */ } elseif ($message['type'] == 'sms') { /* Телеграмма */
echo sprintf("<span style='color:darkgreen;background:#afa;'>[Телеграмма]: %s %s</span>", $d->format('H:i'),$m).PHP_EOL; echo sprintf("<span style='color:darkgreen;background:#afa;'>[Телеграмма]: %s %s</span>", $d->format('H:i'), $m) . PHP_EOL;
} else {echo sprintf('%s [%s]: %s', $d->format('H:i'),$message['from'],$m).PHP_EOL;} } else {
echo sprintf('%s [%s]: %s', $d->format('H:i'), $message['from'], $m) . PHP_EOL;
}
}
} catch (\Krugozor\Database\Mysql\Exception $e) {
echo "<div style='background-color: #ffaaaa;'>Ошибка: " . $e->getMessage() . "<br> В файле: " . $e->getFile() . " (" . $e->getLine() . ")</div>";
} }
} }
show_messages(); show_messages();
?> ?>
<!--<!doctype html>--> <!--<!doctype html>-->