Learn thinkphp5's unpredictable internal abnormal api data output solution
Original error exception
Create an error in the save method, as follows, no data variable, but echo,
//post 需要定义$id
public function save()
{
echo $data;
$post_data = input('post.');
$data = array(
'post_data' => $post_data
);
return show(0, '保存成功!', $data);
}
Request an error, throw an exception
Comes with an error exception, which is the render method of the Handle.php class in \thinkphp\library\think\exception in the root directory of the call
public function render(Exception $e)
{
if ($e instanceof HttpException) {
return $this->renderHttpException($e);
} else {
return $this->convertExceptionToResponse($e);
}
}
We need to create a new class, re-render the render method, and change the way of throwing exceptions
Encapsulate the error exception class
1. Create the file ApiHandleException.php
Create the ApiHandleException.php file in the thinkphpwu\application\common\lib\exception directory. The
specific code is as follows:
<?php
namespace app\common\lib\exception;
//引用异常类
use think\exception\Handle;
//继承异常类
class ApiHandleException extends Handle
{
//自定义http状态码
public static $httpCode = 500;
//覆盖原有render方法
public function render(\Exception $e)
{
return show(0, $e->getMessage(), [], self::$httpCode);
}
}
2. Modify the config.php configuration
Find exception_handle in config.php under application directory
modify:
// 异常处理handle类 留空使用 \think\exception\Handle
'exception_handle' => 'app\common\lib\exception\ApiHandleException',
postman test
Package business throws an exception class
When we perform business, we will encounter exception handling that does not meet the requirements of the background judgment and throw it. If we only use the exception class of the framework itself, it will not meet the business needs.
For example, we modify a save method
//post 需要定义$id
public function save()
{
$post_data = input('post.');
if ($post_data['url'] != 'qipa250.com') {
//抛出异常
exception('域名不正确');
}
$data = array(
'post_data' => $post_data
);
return show(0, '保存成功!', $data);
}
When we request, we will find that the http status code is 500, which is unreasonable. We need to encapsulate the exception class to define the httpcode code
Create the ApiException.php class under application\common\lib\exception in the root directory. The
specific code is as follows:
<?php
namespace app\common\lib\exception;
//引用异常类
use think\Exception;
//继承异常类
class ApiException extends Exception
{
//自定义http状态码
public $message = '';
public $httpCode = 400;
public $code = 0;
/*
* 渲染抛出异常的状态码和信息
*/
public function __construct($message = "", $httpCode = 0, $code = 0)
{
$this->message = $message;
$this->httpCode = $httpCode;
$this->code = $code;
}
}
Reference in the save method of the controller
//在class的顶部引用
use app\common\lib\exception\ApiException;
//post 需要定义$id
public function save()
{
$post_data = input('post.');
if ($post_data['url'] != 'qipa250.com') {
//抛出异常
throw new ApiException('域名不正确', 444, 1);
}
$data = array(
'post_data' => $post_data
);
return show(0, '保存成功!', $data);
}
Modify the render method in the ApiHandleException class
//覆盖原有render方法
public function render(\Exception $e)
{
//如果debug模式开启,则直接返回报错信息
if (config('app_debug') == true) {
return parent::render($e);
}
//否则debug模式关闭,返回格式化的报错信息
if ($e instanceof ApiException) {
self::$httpCode = $e->httpCode;
}
return show(0, $e->getMessage(), [], self::$httpCode);
}
Request again, you can see that the value of httpcode has been modified
debug mode on and off
In the config.php configuration file of the application directory, find app_debug
// 应用调试模式
'app_debug' => false, //false为关闭,true为开启
Turn off the debugging mode and return the following content:
Turn on the debugging mode, and the following content will be returned: You
can see the error message more intuitively. In the production environment, you must turn off the debugging mode