Skip to content

Commit

Permalink
fix(response): fix output multi json struct when a fetal error occur 🚑
Browse files Browse the repository at this point in the history
  • Loading branch information
TIGERB committed Jan 6, 2019
1 parent 3c14be4 commit 1aaa29a
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 21 deletions.
4 changes: 4 additions & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,10 @@ cp ./.git-hooks/* ./git/hooks

# DONE

- v0.8.5(2018/01/06)
- 使用error_report并设置为0,使得错误统一由框架的错误handle处理
- 修复当__coreError发生时,会把__coreError和正常返回值同事输出的问题

- v0.8.1(2018/06/24)
- 重构日志类
- 增加bin目录统一存放脚本文件
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,8 @@ project address: [https://github.com/TIGERB/easy-php](https://github.com/TIGERB/

- v0.8.5(2018/01/06)
- fix error_report
- fix

- fix when __coreError is occur the response is output 200 but it also out put __coreError
- v0.8.1(2018/06/24)
- use easy log
- add folder bin
Expand Down
23 changes: 20 additions & 3 deletions framework/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,22 +258,39 @@ public function run(Closure $request)
* @return json
*/
public function response(Closure $closure)
{
/**
* 错误处理handle里 fatal error是通过register_shutdown_function注册的函数获取的
* 防止fatal error时输出两会json 所以response也注册到register_shutdown_function的队列中
*
* TODO 这个地方要重构
*/
register_shutdown_function([$this, 'responseShutdownFun'], $closure);
}

/**
* shutdown response
*
* @param Closure $closure
* @return void
*/
public function responseShutdownFun(Closure $closure)
{
if ($this->notOutput === true) {
return;
}
if ($this->runningMode === 'cli') {
$closure()->cliModeSuccess($this->responseData);
$closure($this)->cliModeSuccess($this->responseData);
return;
}

$useRest = self::$container->getSingle('config')
->config['rest_response'];

if ($useRest) {
$closure()->restSuccess($this->responseData);
$closure($this)->restSuccess($this->responseData);
}
$closure()->response($this->responseData);
$closure($this)->response($this->responseData);
}

/**
Expand Down
1 change: 1 addition & 0 deletions framework/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Framework;

use Framework\App;
use Framework\Exceptions\CoreHttpException;

/**
Expand Down
14 changes: 12 additions & 2 deletions framework/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,29 @@

namespace Framework;

use Framework\App;

/**
* 响应
*
* @author TIERGB <https://github.com/TIGERB>
*/
class Response
{

/**
* app instance
*
* @var Framework\App
*/
private $app = null;

/**
* 构造函数
*/
public function __construct()
public function __construct(App $app)
{
#code...
$this->app = $app;
}

/**
Expand Down
32 changes: 28 additions & 4 deletions framework/exceptions/CoreHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
*/
class CoreHttpException extends Exception
{

private static $hadException = false;

/**
* 响应异常code
*
Expand Down Expand Up @@ -78,9 +81,19 @@ public function reponse()
// log
Log::error(json_encode($data));

// response
header('Content-Type:Application/json; Charset=utf-8');
die(json_encode($data, JSON_UNESCAPED_UNICODE));
/**
* response
*
* 错误处理handle里 fatal error是通过register_shutdown_function注册的函数获取的
* 防止fatal error时输出两会json 所以response也注册到register_shutdown_function的队列中
*
* TODO 这个地方要重构
*/
register_shutdown_function(function () use ($data)
{
header('Content-Type:Application/json; Charset=utf-8');
die(json_encode($data, JSON_UNESCAPED_UNICODE));
});
}

/**
Expand Down Expand Up @@ -120,6 +133,17 @@ public function reponseSwoole()
*/
public static function reponseErr($e)
{
/**
* 防止同时输出多个错误json
*/
if (self::$hadException) {
// log
Log::error(json_encode($data));
return;
}

self::$hadException = true;

$data = [
'__coreError' => [
'code' => 500,
Expand All @@ -135,7 +159,7 @@ public static function reponseErr($e)
Log::error(json_encode($data));

header('Content-Type:Application/json; Charset=utf-8');
die(json_encode($data));
die(json_encode($data, JSON_UNESCAPED_UNICODE));
}

/**
Expand Down
8 changes: 8 additions & 0 deletions framework/handles/ErrorHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class ErrorHandle implements Handle
*/
private $mode = 'fmp';

/**
* app instance
*
* @var Framework\App
*/
private $app = null;

/**
* 错误信息
*
Expand All @@ -56,6 +63,7 @@ public function __construct()
public function register(App $app)
{
$this->mode = $app->runningMode;
$this->app = $app;

// do not report the error by php self
// E_ERROR | E_WARNING | E_PARSE | E_NOTICE | E_ALL
Expand Down
28 changes: 18 additions & 10 deletions framework/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,42 +52,50 @@
* Load all kinds of handles
*/
$app->load(function () {
// 加载预环境参数机制 Loading env handle
// 加载预环境参数机制
// Loading env handle
return new EnvHandle();
});

$app->load(function () {
// 加载预定义配置机制 Loading config handle
// 加载预定义配置机制
// Loading config handle
return new ConfigHandle();
});

$app->load(function () {
// 加载日志处理机制 Loading log handle
// 加载日志处理机制
// Loading log handle
return new LogHandle();
});

$app->load(function () {
// 加载错误处理机制 Loading error handle
// 加载错误处理机制
// Loading error handle
return new ErrorHandle();
});

$app->load(function () {
// 加载异常处理机制 Loading exception handle.
// 加载异常处理机制
// Loading exception handle
return new ExceptionHandle();
});

$app->load(function () {
// 加载nosql机制 Loading nosql handle
// 加载nosql机制
// Loading nosql handle
return new NosqlHandle();
});

$app->load(function () {
// 加载用户自定义机制 Loading user-defined handle
// 加载用户自定义机制
// Loading user-defined handle
return new UserDefinedHandle();
});

$app->load(function () {
// 加载路由机制 Loading route handle
// 加载路由机制
// Loading route handle
return new RouterHandle();
});

Expand Down Expand Up @@ -117,8 +125,8 @@
*
* End
*/
$app->response(function () {
return new Response();
$app->response(function ($app) {
return new Response($app);
});
} catch (CoreHttpException $e) {
/**
Expand Down

0 comments on commit 1aaa29a

Please sign in to comment.