php输出大量数据api,[PHP高可用后端]②②--不可预知的内部异常API数据输出解决方案...

9adebc107859

image.png

报错的内容应该以API形式显示

9adebc107859

image.png

9adebc107859

image.png

->必须覆盖render方法

9adebc107859

image.png

ApiHandleException.php

/**

* Created by PhpStorm.

* User: tong

* Date: 2017/11/15

* Time: 14:13

*/

namespace app\common\lib\exception;

use Exception;

use think\exception\Handle;

class ApiHandleException extends Handle

{

/**

* http状态码

* @var int 500 内部错误

*/

public $httpCode = 500;

public function render(Exception $e)

{

return show(0, $e->getMessage(), [], $this->httpCode);

}

}

Test.php

/**

* post 新增

* @return mixed

*/

public function save()

{

if(ids){

echo "test";

}

// try{

// model('asasas');

// }catch(\Exception $e){

// return show(0,$e->getMessage(),400);

// }

//获取到提交数据 插入库

//给客户端APP =》 接口数据

//201 创建成功

return show(1, 'OK', input('post.'), 201);

}

config.php

'exception_handle' => '\app\common\lib\exception\ApiHandleException',

9adebc107859

image.png

Test.php

public function save()

{

$data = input('post.');

if($data['mt']!=1){

exception('您提交的数据不合法',403);

}

//获取到提交数据 插入库

//给客户端APP =》 接口数据

//201 创建成功

return show(1, 'OK', input('post.'), 201);

}

9adebc107859

image.png

状态码仍然是500

=>

ApiHandleException

/**

* Created by PhpStorm.

* User: tong

* Date: 2017/11/15

* Time: 14:13

*/

namespace app\common\lib\exception;

use Exception;

use think\exception\Handle;

class ApiHandleException extends Handle

{

/**

* http状态码

* @var int 500 内部错误

*/

public $httpCode = 500;

public function render(Exception $e)

{

//给服务端看的

if (config('app_debug') == true) {

return parent::render($e);

}

if ($e instanceof ApiException) {

$this->httpCode = $e->httpCode;

}

return show(0, $e->getMessage(), [], $this->httpCode);

}

}

ApiException

/**

* Created by PhpStorm.

* User: tong

* Date: 2017/11/15

* Time: 14:55

*/

namespace app\common\lib\exception;

use think\Exception;

class ApiException extends Exception

{

public $message = '';

public $httpCode = 500;

public $code = 0;

/**

* ApiException constructor.

* @param string $message

* @param int $httpCode

* @param int $code

*/

public function __construct($message = "", $httpCode = 0, $code = 0)

{

$this->httpCode = $httpCode;

$this->message = $message;

$this->code = $code;

}

}

Test.php

/**

* Created by PhpStorm.

* User: tong

* Date: 2017/11/15

* Time: 10:23

*/

namespace app\api\controller;

use app\common\lib\exception\ApiException;

use think\Controller;

class Test extends Controller

{

public function index()

{

return [

'sgsg',

'sgsgs',

];

}

public function update($id = 0)

{

// return $id;//http://singwa.com/test/100

halt(input('put.'));//body提交

}

/**

* post 新增

* @return mixed

*/

public function save()

{

$data = input('post.');

if($data['mt']!=1){

//exception('您提交的数据不合法',403);

throw new ApiException('您提交的数据不合法~~~',403);

}

//获取到提交数据 插入库

//给客户端APP =》 接口数据

//201 创建成功

return show(1, 'OK', input('post.'), 201);

}

}

app_debug==true

9adebc107859

image.png

app_debug==false

9adebc107859

image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值