Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
Easy Http SDK
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
4
Issues
4
List
Board
Labels
Milestones
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Back End
Easy Http SDK
Commits
bdb3a68a
Commit
bdb3a68a
authored
Feb 20, 2023
by
谢宇轩
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: 缩减默认策略的范围
parent
5f2fcc25
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
24 deletions
+27
-24
Application.php
src/Application.php
+22
-18
DefaultErrorHandlingPolicy.php
src/Policy/DefaultErrorHandlingPolicy.php
+5
-6
No files found.
src/Application.php
View file @
bdb3a68a
...
@@ -219,33 +219,37 @@ class Application implements ClientInterface
...
@@ -219,33 +219,37 @@ class Application implements ClientInterface
$rpcResult
=
[];
$rpcResult
=
[];
try
{
if
(
$response
->
getStatusCode
()
!=
304
)
{
$rpcResult
=
$this
->
option
->
errorHandlingPolicy
()
->
process
(
$response
);
try
{
}
catch
(
GuiltyResultException
$exception
)
{
$rpcResult
=
$this
->
option
->
errorHandlingPolicy
()
->
process
(
$response
);
// 没有通过预期判定,转化为 SDK Exception
}
catch
(
GuiltyResultException
$exception
)
{
$this
->
lastRequestContext
[
'error_message'
]
=
$exception
->
getMessage
();
// 没有通过预期判定,转化为 SDK Exception
$this
->
lastRequestContext
[
'error_detail'
]
=
$exception
->
getOptions
();
// 获取错误的详情
$this
->
lastRequestContext
[
'error_message'
]
=
$exception
->
getMessage
();
if
(
$this
->
logger
)
{
$this
->
lastRequestContext
[
'error_detail'
]
=
$exception
->
getOptions
();
// 获取错误的详情
$this
->
logger
->
warning
(
"Error"
,
$this
->
lastRequestContext
);
if
(
$this
->
logger
)
{
}
$this
->
logger
->
warning
(
"Error"
,
$this
->
lastRequestContext
);
throw
new
SdkException
(
$exception
->
getMessage
(),
$request
,
$response
,
$exception
->
getOptions
());
}
}
catch
(
UnknowResultException
$exception
)
{
throw
new
SdkException
(
$exception
->
getMessage
(),
$request
,
$response
,
$exception
->
getOptions
());
// 无法解析,转化为 Application Exception
}
catch
(
UnknowResultException
$exception
)
{
// 日志中保留 OUT PUT
// 无法解析,转化为 Application Exception
$this
->
lastRequestContext
[
'error_message'
]
=
$exception
->
getMessage
();
// 日志中保留 OUT PUT
$this
->
lastRequestContext
[
'error_detail'
]
=
$exception
->
getResult
();
// 获取错误的详情
$this
->
lastRequestContext
[
'error_message'
]
=
$exception
->
getMessage
();
if
(
$this
->
logger
)
{
$this
->
lastRequestContext
[
'error_detail'
]
=
$exception
->
getResult
();
// 获取错误的详情
$this
->
logger
->
error
(
"sdk error, bad response content."
,
$this
->
lastRequestContext
);
if
(
$this
->
logger
)
{
$this
->
logger
->
error
(
"sdk error, bad response content."
,
$this
->
lastRequestContext
);
}
throw
new
ApplicationException
(
"Content Format error."
,
$request
,
[]);
}
}
throw
new
ApplicationException
(
"Content Format error."
,
$request
,
[]);
}
}
$version
=
$response
->
getHeader
(
'ETag'
);
$version
=
$response
->
getHeader
(
'ETag'
);
if
(
!
empty
(
$version
))
{
if
(
!
empty
(
$version
))
{
$rpcResult
=
array_merge
(
$rpcResult
,
[
$rpcResult
=
array_merge
(
$rpcResult
,
[
'version'
=>
$version
[
0
]
'version'
=>
$version
[
0
]
]);
]);
}
}
if
(
$this
->
logger
)
{
if
(
$this
->
logger
)
{
$this
->
logger
->
info
(
"Success"
,
$context
);
$this
->
logger
->
info
(
"Success"
,
$context
);
}
}
...
...
src/Policy/DefaultErrorHandlingPolicy.php
View file @
bdb3a68a
...
@@ -9,15 +9,13 @@ use Psr\Http\Message\ResponseInterface;
...
@@ -9,15 +9,13 @@ use Psr\Http\Message\ResponseInterface;
class
DefaultErrorHandlingPolicy
implements
HandlingPolicyInterface
class
DefaultErrorHandlingPolicy
implements
HandlingPolicyInterface
{
{
/**
/**
* 处理API层面异常的规则
*
* @param ResponseInterface $response
* @param ResponseInterface $response
* @return array<string, mixed>
* @return array<string, mixed>
*/
*/
public
function
process
(
ResponseInterface
$response
)
:
array
public
function
process
(
ResponseInterface
$response
)
:
array
{
{
if
(
$response
->
getStatusCode
()
==
304
)
{
return
[];
}
$responseInfo
=
$response
->
getBody
()
->
getContents
();
$responseInfo
=
$response
->
getBody
()
->
getContents
();
$rpcResult
=
json_decode
(
$responseInfo
,
true
);
$rpcResult
=
json_decode
(
$responseInfo
,
true
);
...
@@ -25,8 +23,9 @@ class DefaultErrorHandlingPolicy implements HandlingPolicyInterface
...
@@ -25,8 +23,9 @@ class DefaultErrorHandlingPolicy implements HandlingPolicyInterface
throw
new
UnknowResultException
(
"Content Format error."
,
$responseInfo
);
throw
new
UnknowResultException
(
"Content Format error."
,
$responseInfo
);
}
}
if
(
!
empty
(
$rpcResult
[
'name'
])
&&
$response
->
getStatusCode
()
>=
400
)
{
$status
=
$rpcResult
[
'status'
]
??
1
;
$errorMessage
=
sprintf
(
"%s : %s"
,
$rpcResult
[
'name'
],
$rpcResult
[
'message'
]
??
""
);
if
(
$status
!=
0
&&
$response
->
getStatusCode
()
>=
400
)
{
$errorMessage
=
sprintf
(
"Api error : %s"
,
$rpcResult
[
'message'
]
??
""
);
throw
new
GuiltyResultException
(
$errorMessage
,
$rpcResult
);
throw
new
GuiltyResultException
(
$errorMessage
,
$rpcResult
);
}
}
return
$rpcResult
;
return
$rpcResult
;
...
...
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