ErrorHandler is configured as an application component in Application by default. You can access that instance via Yii::$app->errorHandler. For more details and usage information on ErrorHandler, see the guide article on handling errors.
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Author: Alexander Makarov (sam@rmcreative.ru)
Author: Carsten Brandt (mail@cebe.cc)
Inheritance: extends Component
Beispiel #1
0
 /**
  * PHP magic method that returns the string representation of this object.
  * @return string the string representation of this object.
  */
 public function __toString()
 {
     // __toString cannot throw exception
     // use trigger_error to bypass this limitation
     try {
         return $this->toString();
     } catch (\Exception $e) {
         ErrorHandler::convertExceptionToError($e);
         return '';
     }
 }
Beispiel #2
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);
         }
     });
 }
Beispiel #3
0
 public function init()
 {
     parent::init();
     $this->parseParams();
     //初始化解析参数
     //重写exception handler
     set_exception_handler(function ($e) {
         //捕获错误信息,stack信息
         $this->exception = \yii\base\ErrorHandler::convertExceptionToString($e);
     });
     //重写register_shutdown
     register_shutdown_function(function () {
         //非致命错误脚本结束写日志
         //完成处理log对象
         if ($this->exception) {
             //异常处理模式跟`return 1`一样
             echo $this->exception, PHP_EOL;
             exit(1);
         }
     });
 }
Beispiel #4
0
 /**
  * PHP magic method that returns the string representation of this object.
  * @see [[$toStringFormat]]
  * @see [[$toStringScheme]]
  * @return string the url of this file.
  */
 public function __toString()
 {
     // __toString cannot throw exception
     // use trigger_error to bypass this limitation
     try {
         if ($this->status === self::STATUS_INITIALIZED_FILE) {
             return $this->getUrl($this->toStringFormat, $this->toStringScheme);
         } else {
             // when validator is generating errors for just uploaded file
             // it adds file object to translation params,
             // but the file cannot get url for just uploaded file
             $name = $this->getName();
             return $name === null ? '' : $name;
         }
     } catch (\Exception $e) {
         ErrorHandler::convertExceptionToError($e);
         return '';
     }
 }
Beispiel #5
0
 /**
  * Dispatches the logged messages to [[targets]].
  * @param array $messages the logged messages
  * @param boolean $final whether this method is called at the end of the current application
  */
 public function dispatch($messages, $final)
 {
     $targetErrors = [];
     foreach ($this->targets as $target) {
         if ($target->enabled) {
             try {
                 $target->collect($messages, $final);
             } catch (\Exception $e) {
                 $target->enabled = false;
                 $targetErrors[] = ['Unable to send log via ' . get_class($target) . ': ' . ErrorHandler::convertExceptionToString($e), Logger::LEVEL_WARNING, __METHOD__, microtime(true), []];
             }
         }
     }
     if (!empty($targetErrors)) {
         $this->dispatch($targetErrors, true);
     }
 }
Beispiel #6
0
 /**
  * Session write handler.
  * Do not call this method directly.
  * @param string $id session ID
  * @param string $data session data
  * @return boolean whether session write is successful
  */
 public function writeSession($id, $data)
 {
     // exception must be caught in session write handler
     // http://us.php.net/manual/en/function.session-set-save-handler.php
     try {
         $this->db->getCollection($this->sessionCollection)->update(['id' => $id], $this->composeFields($id, $data), ['upsert' => true]);
     } catch (\Exception $e) {
         $exception = ErrorHandler::convertExceptionToString($e);
         // its too late to use Yii logging here
         error_log($exception);
         echo $exception;
         return false;
     }
     return true;
 }
Beispiel #7
0
 public function __toString()
 {
     $this->addField();
     // __toString cannot throw exception
     // use trigger_error to bypass this limitation
     try {
         return $this->render();
     } catch (\Exception $e) {
         \yii\base\ErrorHandler::convertExceptionToError($e);
         return '';
     }
 }
Beispiel #8
0
 /**
  * Rotates image if need.
  * @param string $readFilePath path to file that must be checked and rotated (if need).
  * @param string|null|false $extension extension for new temp file.
  * Default is false, meaning extension must be parsed from `$readFilePath` file.
  * @return string|null new temp file path, or null if rotating is not necessary.
  */
 protected function rotateImage($readFilePath, $extension = false)
 {
     try {
         $result = ImageHelper::normalizeRotating($readFilePath);
         if (!$result) {
             return null;
         }
         if ($extension === false) {
             $extension = $this->parseFileExtension($readFilePath);
         }
         $tempFile = FileSystemHelper::getNewTempFilename($extension);
         $result->save($tempFile, $this->saveOptions);
         return $tempFile;
     } catch (\Exception $ex) {
         Yii::error('Cannot normalize rotating. ' . ErrorHandler::convertExceptionToString($ex), __METHOD__);
         return null;
     }
 }