You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pdd-order-api/app/libs/tool/class.JMonitorHandler.php

120 lines
4.1 KiB
PHP

<?php
class JMonitorHandler extends ZcMonitorHandler {
private $log;
public function __construct() {
$this->log = Zc::getLog('jmonitor');
}
public function monitor($errorStr, $errorType = 1, $needNotify = false) {
if (strpos($errorStr, '[' . $errorType . '] [ZcMonitor::appError') !== 0 || $errorType != E_DEPRECATED) {
DbTool::rollbackAllTrans(true);
}
if (strpos($errorStr, 'BaseRuntimeException#') === 0) {
if (strpos($errorStr, 'RedirectException')) {
return $this->handleRedirect($errorStr);
} else {
return $this->handleBaseRuntimeException($errorStr);
}
} elseif (preg_match('/^exception \'(.*Exception)\'/', $errorStr, $matches)) {
$exceptionClass = $matches[1];
return $this->handleException($errorStr, $exceptionClass);
} elseif (strpos($errorStr, '[' . $errorType . '] [ZcMonitor::appError') === 0) {
return $this->handleAppError($errorStr, $errorType);
} else {
return $this->handleException($errorStr, Exception::class);
}
}
protected function getErrorContents($errorStr) {
if (!is_string($errorStr)) {
$errorStr = "\r\n" . print_r($errorStr, true);
}
$errorContents = $errorStr;
$errorContents .= "\r\n <b>hostname:</b> " . php_uname ( 'n' ) . "\r\n";
$errorContents .= '&split&';
if (isset($_SESSION)) {
$errorContents .= "\r\n + - - - - - - - - - - - - - - - - SESSION INFOMATION - - - - - - - - - - - - - - - + \r\n";
$errorContents .= "\r\n" . print_r($_SESSION, true) . "\r\n";
}
$errorContents .= "\r\n + - - - - - - - - - - - - - - - - SERVER INFOMATION - - - - - - - - - - - - - - - + \r\n";
$errorContents .= "\r\n" . print_r($_SERVER, true) . "\r\n";
$errorContents .= "\r\n + - - - - - - - - - - - - - - - - DEBUG BACKTRACE - - - - - - - - - - - - - - - - + \r\n";
$errorContents .= "\r\n". $this->stackTrace() . "\r\n";
return $errorContents;
}
private function stackTrace() {
$stacks = debug_backtrace(false);
$output = 'Stack trace:' . PHP_EOL;
foreach ($stacks as $index => $stack) {
$output .= '#' . $index . ' ' . $stack['file'] . ':' . $stack['line'] . ' - ' . $stack['class'] . $stack['type'] . $stack['function'] . PHP_EOL;
if ($stack['function'] != 'call_user_func_array') {
// $output .= print_r($stack['args'], true) . PHP_EOL;
}
}
return $output;
}
private function handleRedirect($errorStr) {
}
private function handleBaseRuntimeException($errorStr)
{
$exceptionInfo = json_decode(str_replace('BaseRuntimeException#', '', $errorStr), true);
CommonTool::renderException(
$exceptionInfo['errorCode'],
$exceptionInfo['message'],
$exceptionInfo['extendData'],
$exceptionInfo['debugError'],
$exceptionInfo['httpCode'],
$exceptionInfo['exceptionClass']
);
return false;
}
private function handleException($errorStr, $exceptionClass = '')
{
$exception = $exceptionClass ? new $exceptionClass() : null;
$httpCode = 500;
$code = ErrorCodeConst::error;
$message = '服务器内部异常';
if ($exception instanceof TestException) {
$message = '测试异常';
} else {
// 其他异常
}
$content = $this->getErrorContents($errorStr);
$this->log->error($content);
CommonTool::renderException($code, $message, [], $errorStr, $httpCode);
return false;
}
private function handleAppError($errorStr, $errorType)
{
error_reporting(0);
if ($errorType == E_DEPRECATED) {
return false;
}
$content = $this->getErrorContents($errorStr);
$this->log->error($content);
$httpCode = 500;
$errorCode = ErrorCodeConst::systemInternalError;
$message = '服务器内部错误';
CommonTool::renderException($errorCode, $message, [], $errorStr, $httpCode);
return false;
}
}