yii\base\ErrorHandler::convertExceptionToString PHP Метод

convertExceptionToString() публичный статический Метод

Converts an exception into a simple string.
public static convertExceptionToString ( Exception $exception ) : string
$exception Exception the exception being converted
Результат string the string representation of the exception.
    public static function convertExceptionToString($exception)
    {
        if ($exception instanceof Exception && ($exception instanceof UserException || !YII_DEBUG)) {
            $message = "{$exception->getName()}: {$exception->getMessage()}";
        } elseif (YII_DEBUG) {
            if ($exception instanceof Exception) {
                $message = "Exception ({$exception->getName()})";
            } elseif ($exception instanceof ErrorException) {
                $message = "{$exception->getName()}";
            } else {
                $message = 'Exception';
            }
            $message .= " '" . get_class($exception) . "' with message '{$exception->getMessage()}' \n\nin " . $exception->getFile() . ':' . $exception->getLine() . "\n\n" . "Stack trace:\n" . $exception->getTraceAsString();
        } else {
            $message = 'Error: ' . $exception->getMessage();
        }
        return $message;
    }

Usage Example

Пример #1
0
 public function init()
 {
     parent::init();
     $this->parseParams();
     //初始化解析参数
     //重写exception handler
     set_exception_handler(function ($e) {
         //捕获错误信息
         $this->exception = \yii\base\ErrorHandler::convertExceptionToString($e);
     });
     //重写register_shutdown
     register_shutdown_function(function () {
         //非致命错误脚本结束写日志
         $task = isset($this->data['task']) ? $this->data['task'] : '';
         $task_id = isset($this->data['info']['task_id']) ? $this->data['info']['task_id'] : 0;
         $orgcode = isset($this->data['info']['orgcode']) ? $this->data['info']['orgcode'] : '';
         $task_type = isset($this->data['info']['task_type']) ? $this->data['info']['task_type'] : '';
         $start_time = isset($this->data['info']['start_time']) ? $this->data['info']['start_time'] : '';
         $msg = [];
         $msg['info'] = $this->loginfo['info'];
         $msg['exitStatus'] = \Yii::$app->response->exitStatus;
         if (isset($this->data['retry_times'])) {
             $msg['retry_times'] = $this->data['retry_times'];
         }
         $end_time = date('Y-m-d H:i:s');
         if ($this->exception) {
             $msg['exitStatus'] = 1;
             $IsSuccess = 0;
         } elseif ($msg['exitStatus'] == 0) {
             $IsSuccess = 1;
         } elseif ($msg['exitStatus'] == 1) {
             $IsSuccess = 0;
         }
         $this->task_log($task_id, $orgcode, $task, $task_type, $start_time, $end_time, $msg, $IsSuccess, $this->exception);
         if ($IsSuccess) {
             //成功则更细
             $this->updateTaskStatus($task_id, ['progress' => 0, 'retry_times' => 0]);
             //更新任务状态
         } else {
             //失败则更新
             $retry_times = isset($this->data['retry_times']) ? $this->data['retry_times'] : 0;
             //                 $this->updateTaskStatus($task_id,['retry_times'=>$retry_times]); //更新任务状态,重试次数
             $this->updateTaskStatus($task_id, ['progress' => 0, 'retry_times' => 0]);
             //更新任务状态
         }
         if ($this->exception) {
             echo $this->exception, PHP_EOL;
             exit(1);
         }
     });
 }
All Usage Examples Of yii\base\ErrorHandler::convertExceptionToString