getPrevious() ) ){ $current = $next; } return $current; } /** * @return string */ public function getRealFile(){ if( $this->_file ){ return $this->_file; } return $this->getRootException()->getFile(); } /** * @return int */ public function getRealLine(){ if( $this->_line ){ return $this->_line; } return $this->getRootException()->getLine(); } /** * @return array */ public function getRealTrace(){ return $this->getRootException()->getTrace(); } /** * @param int $depth number of levels up from callee * @return Loco_error_Exception */ public function setCallee( $depth = 0 ){ $stack = debug_backtrace(0); $callee = $stack[$depth]; $this->_file = $callee['file']; $this->_line = $callee['line']; // TODO could also log the stack trace from $depth upwards, but not required unless being logged or thrown return $this; } /** * Write this error to file regardless of log level * @return void */ public function log(){ $file = new Loco_fs_File( $this->getRealFile() ); $path = $file->getRelativePath( loco_plugin_root() ); $text = sprintf('[Loco.%s] "%s" in %s:%u', $this->getType(), $this->getMessage(), $path, $this->getRealLine() ); // separate error log for cli tests if( 'cli' === PHP_SAPI && defined('LOCO_TEST_DATA_ROOT') ){ error_log( '['.date('c').'] '.$text."\n", 3, 'debug.log' ); } // Else write to default PHP log, but note that WordPress may have set this to wp-content/debug.log. // If no `error_log` is set this will send message to the SAPI, so check your httpd/fast-cgi errors too. else { error_log( $text, 0 ); } } /** * Get view template for rendering error to HTML. * @return string path relative to root tpl directory */ public function getTemplate(){ return 'admin/errors/generic'; } /** * Get notice level short code as a string * @return string */ public function getType(){ return 'error'; } /** * Get verbosity level * @return int */ public function getLevel(){ return self::LEVEL_ERROR; } /** * Call wp cli logging function * @return void */ public function logCli(){ WP_CLI::error( $this->getMessage(), false ); } /** * Get localized notice level name * @return string */ public function getTitle(){ return __('Error','loco-translate'); } /** * @return array */ #[ReturnTypeWillChange] public function jsonSerialize(){ return [ 'code' => $this->getCode(), 'type' => $this->getType(), 'class' => get_class($this), 'title' => $this->getTitle(), 'message' => $this->getMessage(), //'file' => str_replace( ABSPATH, '', $this->getRealFile() ), //'line' => $this->getRealLine() ]; } /** * Push navigation links into error. Use for help pages etc.. * @param string $href * @param string $text * @return Loco_error_Exception */ public function addLink( $href, $text ){ $this->links[] = sprintf('%s', esc_url($href), esc_html($text) ); return $this; } /** * @return array */ public function getLinks(){ return $this->links; } /** * Convert generic exception to one of ours * @param Exception $e original error * @return Loco_error_Exception */ public static function convert( Exception $e ){ if( $e instanceof Loco_error_Exception ){ return $e; } return new Loco_error_Exception( $e->getMessage(), $e->getCode(), $e ); } /** * Test if this error should be automatically logged * @return bool */ public function loggable(){ if( $this->_log ){ // Log messages of minimum priority and up, depending on debug mode // note that non-debug level is in line with error_reporting set by WordPress (notices ignored) $priority = loco_debugging() ? Loco_error_Exception::LEVEL_DEBUG : Loco_error_Exception::LEVEL_WARNING; return $this->getLevel() <= $priority; } return false; } /** * Suppress logging for this error. e.g if you want to warn in UI but don't want to pollute log files. * @return self */ public function noLog(){ $this->_log = false; return $this; } }