Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-rpc
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
0
Merge Requests
0
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-rpc
Commits
10a3ef90
Commit
10a3ef90
authored
Feb 07, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加自定义异常和异常处理文件
parent
ae89acf9
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
92 additions
and
43 deletions
+92
-43
MicroExceptionHandler.php
src/Exceptions/Handler/MicroExceptionHandler.php
+57
-0
HttpResponseException.php
src/Exceptions/HttpResponseException.php
+16
-0
RepositoryException.php
src/Exceptions/RepositoryException.php
+16
-0
ValidatorException.php
src/Exceptions/ValidatorException.php
+1
-1
AbstractValidator.php
src/Validator/AbstractValidator.php
+1
-1
ValidatorInterface.php
src/Validator/Contracts/ValidatorInterface.php
+1
-1
ValidatorExceptionHandler.php
src/Validator/Exceptions/ValidatorExceptionHandler.php
+0
-40
No files found.
src/Exceptions/Handler/MicroExceptionHandler.php
0 → 100644
View file @
10a3ef90
<?php
/**
* Created by PhpStorm.
* User: zero
* Date: 2020/2/7
* Time: 16:39
*/
namespace
Meibuyu\Micro\Exceptions\Handler
;
use
Hyperf\ExceptionHandler\ExceptionHandler
;
use
Hyperf\HttpMessage\Stream\SwooleStream
;
use
Meibuyu\Micro\Exceptions\HttpResponseException
;
use
Meibuyu\Micro\Exceptions\ValidatorException
;
use
Psr\Http\Message\ResponseInterface
;
use
Throwable
;
class
MicroExceptionHandler
extends
ExceptionHandler
{
public
function
handle
(
Throwable
$throwable
,
ResponseInterface
$response
)
{
// 判断被捕获到的异常是希望被捕获的异常
if
(
$throwable
instanceof
HttpResponseException
)
{
// 格式化输出
$data
=
json_encode
([
'code'
=>
$throwable
->
getCode
()
?:
400
,
'msg'
=>
$throwable
->
getMessage
(),
],
JSON_UNESCAPED_UNICODE
);
// 阻止异常冒泡
$this
->
stopPropagation
();
return
$response
->
withAddedHeader
(
'content-type'
,
'application/json'
)
->
withBody
(
new
SwooleStream
(
$data
));
}
else
if
(
$throwable
instanceof
ValidatorException
)
{
$this
->
stopPropagation
();
// 阻止异常冒泡
/** @var ValidatorException $throwable */
$data
=
json_encode
([
'code'
=>
$throwable
->
getCode
()
?:
401
,
'msg'
=>
$throwable
->
first
(),
],
JSON_UNESCAPED_UNICODE
);
return
$response
->
withAddedHeader
(
'content-type'
,
'application/json'
)
->
withBody
(
new
SwooleStream
(
$data
));
}
return
$response
;
// 交给下一个异常处理器
}
public
function
isValid
(
Throwable
$throwable
)
:
bool
{
return
true
;
}
}
\ No newline at end of file
src/Exceptions/HttpResponseException.php
0 → 100644
View file @
10a3ef90
<?php
/**
* Created by PhpStorm.
* User: zero
* Date: 2020/2/7
* Time: 16:18
*/
namespace
Meibuyu\Micro\Exceptions
;
class
HttpResponseException
extends
\Exception
{
}
\ No newline at end of file
src/Exceptions/RepositoryException.php
0 → 100644
View file @
10a3ef90
<?php
/**
* Created by PhpStorm.
* User: zero
* Date: 2020/2/7
* Time: 15:24
*/
namespace
Meibuyu\Micro\Exceptions
;
class
RepositoryException
extends
\Exception
{
}
\ No newline at end of file
src/
Validator/
Exceptions/ValidatorException.php
→
src/Exceptions/ValidatorException.php
View file @
10a3ef90
<?php
namespace
Meibuyu\Micro\
Validator\
Exceptions
;
namespace
Meibuyu\Micro\Exceptions
;
use
Hyperf\Utils\Contracts\MessageBag
;
...
...
src/Validator/AbstractValidator.php
View file @
10a3ef90
...
...
@@ -3,8 +3,8 @@
namespace
Meibuyu\Micro\Validator
;
use
Hyperf\Utils\MessageBag
;
use
Meibuyu\Micro\Exceptions\ValidatorException
;
use
Meibuyu\Micro\Validator\Contracts\ValidatorInterface
;
use
Meibuyu\Micro\Validator\Exceptions\ValidatorException
;
abstract
class
AbstractValidator
implements
ValidatorInterface
{
...
...
src/Validator/Contracts/ValidatorInterface.php
View file @
10a3ef90
...
...
@@ -3,7 +3,7 @@
namespace
Meibuyu\Micro\Validator\Contracts
;
use
Hyperf\Utils\MessageBag
;
use
Meibuyu\Micro\
Validator\
Exceptions\ValidatorException
;
use
Meibuyu\Micro\Exceptions\ValidatorException
;
interface
ValidatorInterface
{
...
...
src/Validator/Exceptions/ValidatorExceptionHandler.php
deleted
100644 → 0
View file @
ae89acf9
<?php
declare
(
strict_types
=
1
);
/**
* Created by PhpStorm.
* User: zero
* Date: 2020/2/5
* Time: 16:16
*/
namespace
Meibuyu\Micro\Validator\Exceptions
;
use
Hyperf\ExceptionHandler\ExceptionHandler
;
use
Hyperf\HttpMessage\Stream\SwooleStream
;
use
Psr\Http\Message\ResponseInterface
;
use
Throwable
;
class
ValidatorExceptionHandler
extends
ExceptionHandler
{
public
function
handle
(
Throwable
$throwable
,
ResponseInterface
$response
)
{
$this
->
stopPropagation
();
// 阻止异常冒泡
/** @var ValidatorException $throwable */
$data
=
json_encode
([
'code'
=>
$throwable
->
getCode
()
?:
401
,
'msg'
=>
$throwable
->
first
(),
],
JSON_UNESCAPED_UNICODE
);
return
$response
->
withAddedHeader
(
'content-type'
,
'application/json'
)
->
withBody
(
new
SwooleStream
(
$data
));
}
public
function
isValid
(
Throwable
$throwable
)
:
bool
{
return
$throwable
instanceof
ValidatorException
;
}
}
\ 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