AjaxErrorHandler.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace common\components;
  3. use Yii;
  4. use yii\web\ErrorHandler;
  5. use yii\web\Response;
  6. class AjaxErrorHandler extends ErrorHandler
  7. {
  8. /**
  9. * @param $exception
  10. * @return void
  11. */
  12. protected function renderException($exception)
  13. {
  14. if (Yii::$app->has('response')) {
  15. $response = Yii::$app->getResponse();
  16. $response->isSent = false;
  17. } else {
  18. $response = new Response();
  19. }
  20. $response->format = Response::FORMAT_JSON;
  21. $error_code = $exception->getCode();
  22. if ($exception->getCode() == 0) {
  23. $error_code = 1;
  24. }
  25. if ($exception instanceof AjaxException) {
  26. $msg = $exception->getMessage();
  27. } else {
  28. error_log($exception->getMessage());
  29. error_log($exception->getTraceAsString());
  30. $msg = "系统繁忙请稍后重试";
  31. var_dump($exception->getMessage());
  32. var_dump($exception->getTraceAsString());
  33. exit;
  34. }
  35. $response->data = [
  36. 'r' => $error_code,
  37. 'msg' => $msg
  38. ];
  39. $response->setStatusCode(200);
  40. $response->send();
  41. }
  42. }