Send prod errors to my chat
authorCameron Ball <cameron@cameron1729.xyz>
Sun, 13 Jan 2019 10:40:45 +0000 (18:40 +0800)
committerCameron Ball <cameron@cameron1729.xyz>
Sun, 13 Jan 2019 10:40:45 +0000 (18:40 +0800)
src/common.php

index d3561ca..aab5088 100644 (file)
@@ -273,7 +273,6 @@ function getMonthName($monthNum) {
     return ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'][$monthNum-1];
 }
 
-// XXX: Consider renaming these to "is[First/Last]WeekOf[Month/Season]"
 function isStartOfSeason($monthNum, $dayNum) {
     return ($monthNum)%3 == 0 && isStartOfMonth($dayNum);
 }
@@ -511,3 +510,54 @@ function getMessagesFromInbox($inbox, array $rules, $unseenOnly = true) {
         }
     );
 }
+
+function log_error($num, $str, $file, $line, $context = null) {
+    $message = $str . "\n" . $file . "(" . $line . ")";
+    getTelegram()->sendMessage([
+        'chat_id' => array_flip(PARTICIPANT_IDS)['Cam'],
+        'parse_mode' => 'Markdown',
+        'text' => "I think there's a mistake in my programming. I can still run but something went wrong... Here's what I know:\n\n```\n" . $message . "\n```"
+    ]);
+}
+
+function log_exception(Throwable $e) {
+    $message = $e->getMessage() . "\n" . $e->getFile() . "(" .  $e->getLine() . ")";
+    getTelegram()->sendMessage([
+        'chat_id' => array_flip(PARTICIPANT_IDS)['Cam'],
+        'parse_mode' => 'Markdown',
+        'text' => "Oh man, I hurt myself trying to do something and had to stop... Here's what I know:\n\n```\n" . $message . "\n\n" . $e->getTraceAsString() . "\n```"
+    ]);
+    exit();
+}
+
+function check_for_fatal() {
+    $error = error_get_last();
+    if (in_array(
+        $error['type'],
+        [E_PARSE, E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR]
+    )) {
+        $message = $error['message'] . "\n" . $error['file'] . "(" .  $error['line'] . ")";
+        getTelegram()->sendMessage([
+            'chat_id' => array_flip(PARTICIPANT_IDS)['Cam'],
+            'parse_mode' => 'Markdown',
+            'text' => "Oh man, I hurt myself trying to do something and had to stop... Here's what I know:\n\n```\n" . $message . "\n\n" . $e->getTraceAsString() . "\n```"
+        ]);
+        exit();
+    }
+
+    if($error) {
+        $message = $error['message'] . "\n" . $error['file'] . "(" .  $error['line'] . ")";
+        getTelegram()->sendMessage([
+            'chat_id' => array_flip(PARTICIPANT_IDS)['Cam'],
+            'parse_mode' => 'Markdown',
+            'text' => "I think there's a mistake in my programming. I can still run but something went wrong... Here's what I know:\n\n```\n" . $message . "\n```"
+        ]);
+    }
+}
+
+if (SEND_ERRORS_TO_TELEGRAM) {
+    register_shutdown_function( "check_for_fatal" );
+    set_error_handler( "log_error" );
+    set_exception_handler( "log_exception" );
+    error_reporting( E_ALL );
+}