AdminException.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2018/01/10
  6. */
  7. namespace basic;
  8. use Exception;
  9. use service\JsonService;
  10. use think\exception\Handle;
  11. use think\exception\HttpException;
  12. use think\exception\ValidateException;
  13. use think\Log;
  14. use think\Request;
  15. use think\Url;
  16. class AdminException extends Handle
  17. {
  18. public function render(Exception $e){
  19. // 参数验证错误
  20. if ($e instanceof ValidateException) {
  21. return json($e->getError(), 422);
  22. }
  23. // 请求异常
  24. if ($e instanceof HttpException && request()->isAjax()) {
  25. return JsonService::fail('系统错误');
  26. }else{
  27. if(config("app_debug")==true){ //如是开启调试,就走原来的方法
  28. return parent::render($e);
  29. }else {
  30. $title = '系统错误';
  31. $msg = addslashes($e->getMessage());
  32. $this->recordErrorLog($e);
  33. exit(view('public/500', compact('title', 'msg'))->getContent());
  34. }
  35. }
  36. }
  37. /*
  38. * 将异常写入日志
  39. */
  40. private function recordErrorLog(Exception $e) {
  41. Log::init([
  42. 'type' => 'File',
  43. 'path' => LOG_PATH,
  44. 'level' => ['error'],
  45. ]);
  46. Log::record($e->getMessage(), 'error');
  47. }
  48. }