@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 76 lines 2.2 kB view raw
1<?php 2 3final class DarkConsoleErrorLogPluginAPI extends Phobject { 4 5 private static $errors = array(); 6 7 private static $discardMode = false; 8 9 public static function registerErrorHandler() { 10 // NOTE: This forces PhutilReadableSerializer to load, so that we are 11 // able to handle errors which fire from inside autoloaders (PHP will not 12 // reenter autoloaders). 13 PhutilReadableSerializer::printableValue(null); 14 PhutilErrorHandler::setErrorListener( 15 array(self::class, 'handleErrors')); 16 } 17 18 public static function enableDiscardMode() { 19 self::$discardMode = true; 20 } 21 22 public static function disableDiscardMode() { 23 self::$discardMode = false; 24 } 25 26 public static function getErrors() { 27 return self::$errors; 28 } 29 30 public static function handleErrors($event, $value, $metadata) { 31 if (self::$discardMode) { 32 return; 33 } 34 35 switch ($event) { 36 case PhutilErrorHandler::EXCEPTION: 37 // $value is of type Exception 38 self::$errors[] = array( 39 'details' => $value->getMessage(), 40 'event' => $event, 41 'file' => $value->getFile(), 42 'line' => $value->getLine(), 43 'str' => $value->getMessage(), 44 'trace' => $metadata['trace'], 45 ); 46 break; 47 case PhutilErrorHandler::DEPRECATED: 48 case PhutilErrorHandler::ERROR: 49 // $value is a simple string 50 self::$errors[] = array( 51 'details' => $value, 52 'event' => $event, 53 'file' => $metadata['file'], 54 'line' => $metadata['line'], 55 'str' => $value, 56 'trace' => $metadata['trace'], 57 ); 58 break; 59 case PhutilErrorHandler::PHLOG: 60 // $value can be anything 61 self::$errors[] = array( 62 'details' => PhutilReadableSerializer::printShallow($value, 3), 63 'event' => $event, 64 'file' => $metadata['file'], 65 'line' => $metadata['line'], 66 'str' => PhutilReadableSerializer::printShort($value), 67 'trace' => $metadata['trace'], 68 ); 69 break; 70 default: 71 error_log(pht('Unknown event: %s', $event)); 72 break; 73 } 74 } 75 76}