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
981bbae5
Commit
981bbae5
authored
May 12, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加通用外键关联异常捕获
parent
6d9abb9a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
0 deletions
+46
-0
ConfigProvider.php
src/ConfigProvider.php
+1
-0
QueryExceptionHandler.php
src/Exceptions/Handler/QueryExceptionHandler.php
+45
-0
No files found.
src/ConfigProvider.php
View file @
981bbae5
...
...
@@ -18,6 +18,7 @@ class ConfigProvider
'handler'
=>
[
'http'
=>
[
\Meibuyu\Micro\Exceptions\Handler\MicroExceptionHandler
::
class
,
\Meibuyu\Micro\Exceptions\Handler\QueryExceptionHandler
::
class
,
],
],
],
...
...
src/Exceptions/Handler/QueryExceptionHandler.php
0 → 100644
View file @
981bbae5
<?php
/**
* Created by PhpStorm.
* User: zero
* Date: 2020/5/12
* Time: 16:11
*/
namespace
Meibuyu\Micro\Exceptions\Handler
;
use
Hyperf\Database\Exception\QueryException
;
use
Hyperf\ExceptionHandler\ExceptionHandler
;
use
Hyperf\HttpMessage\Stream\SwooleStream
;
use
Psr\Http\Message\ResponseInterface
;
use
Throwable
;
class
QueryExceptionHandler
extends
ExceptionHandler
{
public
function
handle
(
Throwable
$throwable
,
ResponseInterface
$response
)
{
$code
=
$throwable
->
getCode
();
$msg
=
$throwable
->
getMessage
();
if
(
$code
==
23000
&&
strpos
(
$msg
,
'foreign key'
)
!==
false
)
{
// 格式化输出
$data
=
json_encode
([
'code'
=>
400
,
'msg'
=>
'此数据下有关联的数据,不可进行操作'
,
],
JSON_UNESCAPED_UNICODE
);
// 阻止异常冒泡
$this
->
stopPropagation
();
return
$response
->
withAddedHeader
(
'content-type'
,
'application/json'
)
->
withBody
(
new
SwooleStream
(
$data
));
}
return
$response
;
// 交给下一个异常处理器
}
public
function
isValid
(
Throwable
$throwable
)
:
bool
{
return
$throwable
instanceof
QueryException
;
}
}
\ No newline at end of file
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