Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-micro
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-micro
Commits
df76c0ea
Commit
df76c0ea
authored
Jun 09, 2021
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
处理异常捕获处理器配置注入
parent
74014251
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
6 deletions
+19
-6
ConfigProvider.php
src/ConfigProvider.php
+4
-1
AppExceptionHandler.php
src/Exceptions/Handler/AppExceptionHandler.php
+15
-5
No files found.
src/ConfigProvider.php
View file @
df76c0ea
...
@@ -15,12 +15,15 @@ class ConfigProvider
...
@@ -15,12 +15,15 @@ class ConfigProvider
{
{
return
[
return
[
'exceptions'
=>
[
'exceptions'
=>
[
/**
* 以下异常处理器会合并到项目的config/autoload/exceptions.php文件配置数组的前面;
* 请勿在此使用顶级异常捕获处理器,防止项目中异常处理器无效;
*/
'handler'
=>
[
'handler'
=>
[
'http'
=>
[
'http'
=>
[
\Meibuyu\Micro\Exceptions\Handler\MicroExceptionHandler
::
class
,
\Meibuyu\Micro\Exceptions\Handler\MicroExceptionHandler
::
class
,
\Meibuyu\Micro\Exceptions\Handler\QueryExceptionHandler
::
class
,
\Meibuyu\Micro\Exceptions\Handler\QueryExceptionHandler
::
class
,
\Meibuyu\Micro\Exceptions\Handler\PhpSpreadsheetExceptionHandler
::
class
,
\Meibuyu\Micro\Exceptions\Handler\PhpSpreadsheetExceptionHandler
::
class
,
\Meibuyu\Micro\Exceptions\Handler\AppExceptionHandler
::
class
,
],
],
],
],
],
],
...
...
src/Exceptions/Handler/AppExceptionHandler.php
View file @
df76c0ea
...
@@ -10,6 +10,7 @@ use Hyperf\Contract\ConfigInterface;
...
@@ -10,6 +10,7 @@ use Hyperf\Contract\ConfigInterface;
use
Hyperf\Contract\StdoutLoggerInterface
;
use
Hyperf\Contract\StdoutLoggerInterface
;
use
Hyperf\ExceptionHandler\ExceptionHandler
;
use
Hyperf\ExceptionHandler\ExceptionHandler
;
use
Hyperf\HttpMessage\Stream\SwooleStream
;
use
Hyperf\HttpMessage\Stream\SwooleStream
;
use
Hyperf\HttpServer\Contract\RequestInterface
;
use
Hyperf\Logger\LoggerFactory
;
use
Hyperf\Logger\LoggerFactory
;
use
Meibuyu\Micro\Amqp\Producer\ExceptionLogProducer
;
use
Meibuyu\Micro\Amqp\Producer\ExceptionLogProducer
;
use
Psr\Container\ContainerInterface
;
use
Psr\Container\ContainerInterface
;
...
@@ -40,24 +41,33 @@ class AppExceptionHandler extends ExceptionHandler
...
@@ -40,24 +41,33 @@ class AppExceptionHandler extends ExceptionHandler
*/
*/
protected
$config
;
protected
$config
;
/**
* @var RequestInterface
*/
protected
$request
;
public
function
__construct
(
ContainerInterface
$container
)
public
function
__construct
(
ContainerInterface
$container
)
{
{
$this
->
container
=
$container
;
$this
->
container
=
$container
;
$this
->
stdoutLogger
=
$container
->
get
(
StdoutLoggerInterface
::
class
);
$this
->
stdoutLogger
=
$container
->
get
(
StdoutLoggerInterface
::
class
);
$this
->
logger
=
$container
->
get
(
LoggerFactory
::
class
)
->
get
(
'Uncaught Exception'
);
$this
->
logger
=
$container
->
get
(
LoggerFactory
::
class
)
->
get
(
'Uncaught Exception'
);
$this
->
config
=
$container
->
get
(
ConfigInterface
::
class
);
$this
->
config
=
$container
->
get
(
ConfigInterface
::
class
);
$this
->
request
=
$container
->
get
(
RequestInterface
::
class
);
}
}
public
function
handle
(
Throwable
$throwable
,
ResponseInterface
$response
)
public
function
handle
(
Throwable
$throwable
,
ResponseInterface
$response
)
{
{
// 捕获所有未捕获的异常
// 捕获所有未捕获的异常
$this
->
stopPropagation
();
$this
->
stopPropagation
();
$api
=
sprintf
(
'%s(%s)'
,
$this
->
request
->
getUri
(),
$this
->
request
->
getMethod
());
$exceptionClass
=
get_class
(
$throwable
);
$message
=
$throwable
->
getMessage
();
$message
=
$throwable
->
getMessage
();
$line
=
$throwable
->
getLine
();
$line
=
$throwable
->
getLine
();
$file
=
$throwable
->
getFile
();
$file
=
$throwable
->
getFile
();
$trace
=
$throwable
->
getTraceAsString
();
$code
=
$throwable
->
getCode
();
$code
=
$throwable
->
getCode
();
$trace
=
$throwable
->
getTraceAsString
();
$data
=
[
$data
=
[
'api'
=>
$api
,
'server'
=>
$this
->
config
->
get
(
'app_name'
),
'server'
=>
$this
->
config
->
get
(
'app_name'
),
'file'
=>
$file
,
'file'
=>
$file
,
'line'
=>
$line
,
'line'
=>
$line
,
...
@@ -73,10 +83,10 @@ class AppExceptionHandler extends ExceptionHandler
...
@@ -73,10 +83,10 @@ class AppExceptionHandler extends ExceptionHandler
}
catch
(
Exception
$e
)
{
}
catch
(
Exception
$e
)
{
put_log
(
'异常日志失败; '
.
$e
->
getMessage
(),
'ExceptionLogProducer.log'
);
put_log
(
'异常日志失败; '
.
$e
->
getMessage
(),
'ExceptionLogProducer.log'
);
}
}
$msg
=
sprintf
(
'%s
[%s] in %s'
,
$message
,
$line
,
$fil
e
);
$msg
=
sprintf
(
'%s
: %s(%s) in %s:%s'
,
$exceptionClass
,
$message
,
$code
,
$file
,
$lin
e
);
$
this
->
stdoutLogger
->
error
(
$msg
);
$
error
=
sprintf
(
"API: %s
\n
%s
\n
Stack trace:
\n
%s"
,
$api
,
$msg
,
$trace
);
$this
->
logger
->
error
(
$
msg
);
$this
->
logger
->
error
(
$
error
);
$this
->
stdoutLogger
->
error
(
$
trace
);
$this
->
stdoutLogger
->
error
(
$
error
);
return
$response
->
withStatus
(
500
)
->
withBody
(
new
SwooleStream
(
$msg
));
return
$response
->
withStatus
(
500
)
->
withBody
(
new
SwooleStream
(
$msg
));
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment