From 1c37171862aad719d26c8d53fcd2005be2841f2b Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Sun, 13 Jan 2019 18:40:45 +0800 Subject: [PATCH] Send prod errors to my chat --- src/common.php | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/common.php b/src/common.php index d3561ca..aab5088 100644 --- a/src/common.php +++ b/src/common.php @@ -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 ); +} -- 2.11.0