AdminException.php 1.4 KB

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