AppExceptionHandler.php 935 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace common\exceptions\handler;
  3. use common\exceptions\ResponseException;
  4. class AppExceptionHandler
  5. {
  6. /**
  7. * 异常处理
  8. * @desc
  9. *
  10. * @param \Throwable $throwable
  11. *
  12. * @return string
  13. */
  14. public function handle(\Throwable $throwable)
  15. {
  16. if ($throwable instanceof ResponseException) {
  17. return $throwable->getMessage();
  18. }
  19. // 记录日志...
  20. \Yii::error(json_encode([
  21. 'trace' => $throwable->getTraceAsString(),
  22. 'file' => $throwable->getFile(),
  23. 'line' => $throwable->getLine(),
  24. 'msg' => $throwable->getMessage()
  25. ], JSON_UNESCAPED_UNICODE));
  26. $msg = '系统错误';
  27. if (!YII_DEBUG) {
  28. $msg = sprintf("%s: err: %s, file: %s, line: %d", $msg, $throwable->getMessage(), $throwable->getFile(), $throwable->getLine());
  29. }
  30. return $msg;
  31. }
  32. }