Skip to content

Commit

Permalink
添加:异常dumper
Browse files Browse the repository at this point in the history
  • Loading branch information
dxkite committed Jul 1, 2019
1 parent 2d2b569 commit 5eb3caa
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
16 changes: 14 additions & 2 deletions suda/src/application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
*/
class Application extends ApplicationSource
{
/**
* @var DebugDumper
*/
protected $dumper;

/**
* 准备运行环境
Expand Down Expand Up @@ -67,8 +71,8 @@ protected function prepare(Request $request, Response $response)
$response->getWrapper()->register(ExceptionContentWrapper::class, [Throwable::class]);
$response->getWrapper()->register(TemplateWrapper::class, [RawTemplate::class]);

$debugDumper = new DebugDumper($this, $request, $response);
$debugDumper->register();
$this->dumper = new DebugDumper($this, $request, $response);
$this->dumper->register();

$this->debug->info('{request-time} {remote-ip} {request-method} {request-uri} debug={debug}', [
'remote-ip' => $request->getRemoteAddr(),
Expand All @@ -84,6 +88,13 @@ protected function prepare(Request $request, Response $response)
}
}

/**
* @param Throwable $throwable
*/
public function dumpException($throwable) {
$this->dumper->dumpThrowable($throwable);
}

/**
* 运行程序
*
Expand All @@ -109,6 +120,7 @@ public function run(Request $request, Response $response)
$this->debug->timeEnd('sending response');
} catch (Throwable $e) {
$this->debug->uncaughtException($e);
$this->dumper->dumpThrowable($e);
$response->sendContent($e);
$response->end();
$this->debug->timeEnd('sending response');
Expand Down
30 changes: 30 additions & 0 deletions suda/src/application/DebugDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
namespace suda\application;

use Exception;
use phpDocumentor\Reflection\File;
use suda\framework\debug\DebugObject;
use suda\framework\filesystem\FileSystem;
use suda\framework\Request;
use suda\framework\Response;
use Throwable;
Expand Down Expand Up @@ -63,9 +66,36 @@ public function uncaughtException($exception)
{
$this->application->debug()->addIgnorePath(__FILE__);
$this->application->debug()->uncaughtException($exception);
$this->dumpThrowable($exception);
if ($this->response->isSend() === false) {
$this->response->sendContent($exception);
$this->response->end();
}
}

/**
* @param Throwable $throwable
*/
public function dumpThrowable($throwable)
{
$dumper = [
'time' => time(),
'throwable' => $throwable,
'context' => [
'application' => $this->application,
'request' => $this->request,
'response' => $this->response,
],
'backtrace' => $throwable->getTrace(),
];
$dumpPath = $this->application->getDataPath().'/logs/dump';
$exceptionHash = md5($throwable->getFile().$throwable->getLine().$throwable->getCode());
$path = $dumpPath.'/'.microtime(true).'.'.substr($exceptionHash, 0, 8).'.json';
FileSystem::make($dumpPath);
FileSystem::put($path, json_encode(
new DebugObject($dumper),
JSON_PRETTY_PRINT
| JSON_UNESCAPED_UNICODE
));
}
}
4 changes: 3 additions & 1 deletion suda/src/framework/debug/ConfigTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public function applyConfig(array $config)
$this->config[$name] = $config[$name] ?? $this->config[$name] ?? $value;
}
}
public function getConfig(string $name) {

public function getConfig(string $name)
{
$defaultConfig = $this->getDefaultConfig();
return $this->config[$name] ?? $defaultConfig[$name];
}
Expand Down
4 changes: 2 additions & 2 deletions suda/src/framework/debug/DebugObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ protected function parseObject($object)
{
$objectHash = spl_object_hash($object);
if ($this->isObjectExported($objectHash)) {
return ['_type' => get_class($object), '_address' => $objectHash];
return ['_type' => get_class($object), '_hash' => $objectHash];
}
$this->setObjectIsExported($objectHash);
return [
'_type' => get_class($object),
'_address' => $objectHash,
'_hash' => $objectHash,
'_properties' => $this->getObjectProp($object)
];
}
Expand Down

0 comments on commit 5eb3caa

Please sign in to comment.