Blogger Information
Blog 6
fans 0
comment 0
visits 8848
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
tp6 全局异常处理
过夜茶
Original
2112 people have browsed it

在写api接口时,当从服务器返回的值不是我们期望的值时都属于异常。包括了非服务器异常(用户行为造成的),服务器异常。为了更直观的用json型式通知给用户,需要定义类进行接管

  1. 需要在app目录下面的provider.php文件中绑定异常处理类
    // 绑定自定义异常处理handle类
    ‘think\exception\Handle’ => ‘\app\lib\exception\ExceptionHandler’

2.在app目录下创建目录lib\exception\
并创建两个类ExceptionHandler BaseException

  1. <?php
  2. namespace app\lib\exception;
  3. use think\exception\Handle;
  4. use think\facade\Env;
  5. use think\Response;
  6. use Throwable;
  7. class ExceptionHandler extends Handle
  8. {
  9. private $msg ="服务器异常";
  10. private $httpcode =500;
  11. private $errorcode =19999;
  12. public function render($request, Throwable $e): Response
  13. {
  14. // 是否处于调试状态下,当处于调试状态下就返回html格式的错误信息。否则返回json格式的错误提示
  15. if(Env::get("APP_DEBUG")==1 && !$e->getCode())
  16. {
  17. // 其他错误交给系统处理
  18. return parent::render($request, $e);
  19. }
  20. // 如果是服务器异常返回预先设置好的
  21. if($e instanceof BaseException){
  22. $this->msg = $e->getMessage()?:$this->msg;
  23. $this->httpcode = $e->getStatusCode()?:$this->httpcode;
  24. $this->errorcode = $e->getCode()?:$this->errorcode;
  25. }
  26. // 如果是服务器异常就返回服务器异常提示
  27. $result_data = [
  28. 'message'=>$this->msg,
  29. 'data'=>[],
  30. 'errorcode'=>$this->errorcode
  31. ];
  32. return json($result_data,$this->httpcode);
  33. }
  34. }
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2021 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\lib\exception;
  13. use Exception;
  14. /**
  15. * HTTP异常
  16. */
  17. class BaseException extends \RuntimeException
  18. {
  19. private $statusCode;
  20. private $headers;
  21. public function __construct(int $statusCode, string $message = '', $code = 0, Exception $previous = null, array $headers = [])
  22. {
  23. $this->statusCode = $statusCode;
  24. $this->headers = $headers;
  25. parent::__construct($message, $code, $previous);
  26. }
  27. public function getStatusCode()
  28. {
  29. return $this->statusCode;
  30. }
  31. public function getHeaders()
  32. {
  33. return $this->headers;
  34. }
  35. }

在控制器中需要抛出异常的地方写入
throw new BaseException(400,”异常描述”,19999);

即可

BaseException类是对HttpException类初始化参数位置作了调整,对HttpException 中的__construct(int $statusCode, string $message = ‘’, Exception $previous = null, array $headers = [], $code = 0)

改成
__construct(int $statusCode, string $message = ‘’, $code = 0, Exception $previous = null, array $headers = [])

当实例化时后面不需要的参数可以省略不填,代码更加简洁

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post