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
66e213ae
Commit
66e213ae
authored
Feb 20, 2023
by
谢宇轩
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: 重构错误处理
parent
3dfc9861
Changes
12
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
290 additions
and
51 deletions
+290
-51
Application.php
src/Application.php
+23
-28
GuiltyResultException.php
src/Exception/GuiltyResultException.php
+33
-0
UnknowResultException.php
src/Exception/UnknowResultException.php
+32
-0
Context.php
src/Http/Context.php
+90
-0
EtagMiddleware.php
src/Middleware/EtagMiddleware.php
+2
-1
JwtMiddleware.php
src/Middleware/JwtMiddleware.php
+2
-1
MiddlewareInterface.php
src/Middleware/MiddlewareInterface.php
+2
-1
RequestIDMiddleware.php
src/Middleware/RequestIDMiddleware.php
+2
-1
Option.php
src/Option.php
+37
-18
DefaultErrorHandlingPolicy.php
src/Policy/DefaultErrorHandlingPolicy.php
+34
-0
HandlingPolicyInterface.php
src/Policy/HandlingPolicyInterface.php
+14
-0
XXXSDKOption.php
src/XXXSDKOption.php
+19
-1
No files found.
src/Application.php
View file @
66e213ae
...
@@ -7,11 +7,12 @@ use GuzzleHttp\Client;
...
@@ -7,11 +7,12 @@ use GuzzleHttp\Client;
use
GuzzleHttp\Exception\GuzzleException
;
use
GuzzleHttp\Exception\GuzzleException
;
use
GuzzleHttp\Exception\TransferException
;
use
GuzzleHttp\Exception\TransferException
;
use
GuzzleHttp\HandlerStack
;
use
GuzzleHttp\HandlerStack
;
use
GuzzleHttp\Psr7\Request
;
use
InvalidArgumentException
;
use
InvalidArgumentException
;
use
Jiwei\EasyHttpSdk\Exception\ApplicationException
;
use
Jiwei\EasyHttpSdk\Exception\ApplicationException
;
use
Jiwei\EasyHttpSdk\Exception\GuiltyResultException
;
use
Jiwei\EasyHttpSdk\Exception\SdkException
;
use
Jiwei\EasyHttpSdk\Exception\SdkException
;
use
Jiwei\EasyHttpSdk\Exception\TimeOutExcetpion
;
use
Jiwei\EasyHttpSdk\Exception\TimeOutExcetpion
;
use
Jiwei\EasyHttpSdk\Exception\UnknowResultException
;
use
Jiwei\EasyHttpSdk\Http\SdkRequest
;
use
Jiwei\EasyHttpSdk\Http\SdkRequest
;
use
Jiwei\EasyHttpSdk\Middleware\JwtMiddleware
;
use
Jiwei\EasyHttpSdk\Middleware\JwtMiddleware
;
use
Psr\Cache\CacheItemPoolInterface
;
use
Psr\Cache\CacheItemPoolInterface
;
...
@@ -29,14 +30,12 @@ class Application implements ClientInterface
...
@@ -29,14 +30,12 @@ class Application implements ClientInterface
private
const
TOKEN_CACHE_KEY
=
"Auth.%s"
;
private
const
TOKEN_CACHE_KEY
=
"Auth.%s"
;
private
const
EXPIRES_AT
=
30000
;
/** @var string jwtToken */
private
$jwtToken
=
""
;
/** @var Option $option */
/** @var Option $option */
private
$option
;
private
$option
;
/** @var string jwtToken */
private
$jwtToken
=
""
;
/** @var null|LoggerInterface 日志记录器 */
/** @var null|LoggerInterface 日志记录器 */
protected
$logger
=
null
;
protected
$logger
=
null
;
...
@@ -194,6 +193,7 @@ class Application implements ClientInterface
...
@@ -194,6 +193,7 @@ class Application implements ClientInterface
public
function
resultDecode
(
SdkRequest
$request
,
ResponseInterface
$response
)
:
array
public
function
resultDecode
(
SdkRequest
$request
,
ResponseInterface
$response
)
:
array
{
{
// context 是没有处理前的上下文
$context
=
$this
->
lastRequestContext
;
$context
=
$this
->
lastRequestContext
;
if
(
$context
[
'request'
])
{
if
(
$context
[
'request'
])
{
...
@@ -219,31 +219,28 @@ class Application implements ClientInterface
...
@@ -219,31 +219,28 @@ class Application implements ClientInterface
$rpcResult
=
[];
$rpcResult
=
[];
if
(
$response
->
getStatusCode
()
!=
304
)
{
try
{
$responseInfo
=
$response
->
getBody
()
->
getContents
();
$rpcResult
=
$this
->
option
->
errorHandlingPolicy
()
->
process
(
$response
);
$rpcResult
=
json_decode
(
$responseInfo
,
true
);
}
catch
(
GuiltyResultException
$exception
)
{
if
(
json_last_error
())
{
// 没有通过预期判定,转化为 SDK Exception
$this
->
lastRequestContext
[
'error_message'
]
=
$exception
->
getMessage
();
$this
->
lastRequestContext
[
'error_detail'
]
=
$exception
->
getOptions
();
// 获取错误的详情
if
(
$this
->
logger
)
{
if
(
$this
->
logger
)
{
$this
->
logger
->
error
(
"sdk error, bad response content."
,
$context
);
$this
->
logger
->
warning
(
"Error"
,
$this
->
lastRequestContext
);
}
throw
new
ApplicationException
(
"Content Format error."
,
$request
,
[]);
}
}
}
throw
new
SdkException
(
$exception
->
getMessage
(),
$request
,
$response
,
$rpcResult
);
$errorName
=
$rpcResult
[
'name'
]
??
""
;
}
catch
(
UnknowResultException
$exception
)
{
// Paypal的API错误结果是通用的,所以这里简单处理一下
// 无法解析,转化为 Application Exception
if
(
!
empty
(
$errorName
)
&&
$response
->
getStatusCode
()
>=
400
)
{
// 日志中保留 OUT PUT
$errorMessage
=
sprintf
(
"%s : %s"
,
$errorName
,
$rpcResult
[
'message'
]
??
""
);
$this
->
lastRequestContext
[
'error_message'
]
=
$exception
->
getMessage
();
$this
->
lastRequestContext
[
'error'
]
=
$errorMessage
;
$this
->
lastRequestContext
[
'error_detail'
]
=
$exception
->
getResult
();
// 获取错误的详情
$this
->
lastRequestContext
[
'error_detail'
]
=
$rpcResult
;
if
(
$this
->
logger
)
{
if
(
$this
->
logger
)
{
$this
->
logger
->
warning
(
"Error
"
,
$this
->
lastRequestContext
);
$this
->
logger
->
error
(
"sdk error, bad response content.
"
,
$this
->
lastRequestContext
);
}
}
throw
new
SdkException
(
$errorMessage
,
$request
,
$response
,
$rpcResult
);
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
]
...
@@ -252,8 +249,6 @@ class Application implements ClientInterface
...
@@ -252,8 +249,6 @@ class Application implements ClientInterface
if
(
$this
->
logger
)
{
if
(
$this
->
logger
)
{
$this
->
logger
->
info
(
"Success"
,
$context
);
$this
->
logger
->
info
(
"Success"
,
$context
);
}
}
return
$rpcResult
;
return
$rpcResult
;
}
}
...
@@ -286,7 +281,7 @@ class Application implements ClientInterface
...
@@ -286,7 +281,7 @@ class Application implements ClientInterface
if
(
!
$currentToken
->
isHit
())
{
if
(
!
$currentToken
->
isHit
())
{
$token
=
$this
->
getAccessToken
(
$app_key
,
$app_secret
);
$token
=
$this
->
getAccessToken
(
$app_key
,
$app_secret
);
$currentToken
->
set
(
$token
)
->
expiresAfter
(
self
::
EXPIRES_AT
);
$currentToken
->
set
(
$token
)
->
expiresAfter
(
$this
->
option
->
getAuthExpires
()
);
$this
->
cache
->
save
(
$currentToken
);
$this
->
cache
->
save
(
$currentToken
);
}
}
...
@@ -303,7 +298,7 @@ class Application implements ClientInterface
...
@@ -303,7 +298,7 @@ class Application implements ClientInterface
private
function
getAccessToken
(
string
$app_key
,
string
$app_secret
)
:
string
private
function
getAccessToken
(
string
$app_key
,
string
$app_secret
)
:
string
{
{
try
{
try
{
$request
=
$this
->
option
->
getAuthMoudel
()(
$app_key
,
$app_secret
);
$request
=
$this
->
option
->
authorization
()(
$app_key
,
$app_secret
);
$response
=
$this
->
client
->
send
(
$request
);
$response
=
$this
->
client
->
send
(
$request
);
}
catch
(
TransferException
$exception
)
{
}
catch
(
TransferException
$exception
)
{
throw
new
RuntimeException
(
'time out!'
);
throw
new
RuntimeException
(
'time out!'
);
...
...
src/Exception/GuiltyResultException.php
0 → 100644
View file @
66e213ae
<?php
namespace
Jiwei\EasyHttpSdk\Exception
;
use
Throwable
;
use
\UnexpectedValueException
;
class
GuiltyResultException
extends
UnexpectedValueException
{
/** @var array<string, mixed> */
private
$options
;
/**
* @param string $message
* @param array<string, mixed> $options
* @param int $code
* @param Throwable|null $previous
*/
public
function
__construct
(
$message
=
""
,
$options
=
[],
$code
=
0
,
Throwable
$previous
=
null
)
{
$this
->
options
=
$options
;
parent
::
__construct
(
$message
,
$code
,
$previous
);
}
/**
* @return array<string, mixed>
*/
public
function
getOptions
()
:
array
{
return
$this
->
options
;
}
}
src/Exception/UnknowResultException.php
0 → 100644
View file @
66e213ae
<?php
namespace
Jiwei\EasyHttpSdk\Exception
;
use
Throwable
;
use
\UnexpectedValueException
;
class
UnknowResultException
extends
UnexpectedValueException
{
/** @var string */
public
$result
;
/**
* @param string $message
* @param string $result
* @param int $code
* @param Throwable|null $previous
*/
public
function
__construct
(
$message
=
""
,
$result
=
""
,
$code
=
0
,
Throwable
$previous
=
null
)
{
$this
->
result
=
$result
;
parent
::
__construct
(
$message
,
$code
,
$previous
);
}
/**
* @return string
*/
public
function
getResult
()
:
string
{
return
$this
->
result
;
}
}
src/Http/Context.php
0 → 100644
View file @
66e213ae
<?php
declare
(
strict_types
=
1
);
namespace
Jiwei\EasyHttpSdk\Http
;
use
ArrayAccess
;
use
GuzzleHttp\Psr7\Response
;
use
Jiwei\EasyHttpSdk\Exception\SdkException
;
/**
* @implements ArrayAccess<string, mixed>
*/
class
Context
implements
ArrayAccess
{
/** @var SdkRequest */
public
$request
;
/** @var Response */
public
$response
;
/** @var int */
public
$requestId
;
/** @var string */
public
$endpoint
;
/** @var string */
public
$action
;
/** @var string */
public
$stage
;
/** @var string */
public
$expend
;
/** @var string */
public
$httpStatus
;
/** @var array<string, mixed> */
public
$data
;
/** @var SdkException */
public
$exception
;
/** @var string */
public
$errorMessage
;
/** @var array<string, mixed>|string */
public
$errorDetail
;
/**
* @param $offset
* @return bool
*/
public
function
offsetExists
(
$offset
)
:
bool
{
return
isset
(
$this
->
$offset
);
}
/**
* @param $offset
* @return mixed
*/
public
function
offsetGet
(
$offset
)
{
return
$this
->
$offset
??
null
;
}
/**
* @param $offset
* @param $value
* @return void
*/
public
function
offsetSet
(
$offset
,
$value
)
{
if
(
in_array
(
$offset
,
get_object_vars
(
$this
)))
{
$this
->
$offset
=
$value
;
}
}
/**
* @param $offset
* @return void
*/
public
function
offsetUnset
(
$offset
)
{
$this
->
$offset
=
null
;
}
}
src/Middleware/EtagMiddleware.php
View file @
66e213ae
<?php
<?php
declare
(
strict_types
=
1
);
namespace
Jiwei\EasyHttpSdk\Middleware
;
namespace
Jiwei\EasyHttpSdk\Middleware
;
...
...
src/Middleware/JwtMiddleware.php
View file @
66e213ae
<?php
<?php
declare
(
strict_types
=
1
);
namespace
Jiwei\EasyHttpSdk\Middleware
;
namespace
Jiwei\EasyHttpSdk\Middleware
;
...
...
src/Middleware/MiddlewareInterface.php
View file @
66e213ae
<?php
<?php
declare
(
strict_types
=
1
);
namespace
Jiwei\EasyHttpSdk\Middleware
;
namespace
Jiwei\EasyHttpSdk\Middleware
;
...
...
src/Middleware/RequestIDMiddleware.php
View file @
66e213ae
<?php
<?php
declare
(
strict_types
=
1
);
namespace
Jiwei\EasyHttpSdk\Middleware
;
namespace
Jiwei\EasyHttpSdk\Middleware
;
...
...
src/Option.php
View file @
66e213ae
<?php
<?php
declare
(
strict_types
=
1
);
namespace
Jiwei\EasyHttpSdk
;
namespace
Jiwei\EasyHttpSdk
;
use
GuzzleHttp\Psr7\Request
;
use
Jiwei\EasyHttpSdk\Policy\HandlingPolicyInterface
;
use
http\Exception\InvalidArgumentException
;
abstract
class
Option
abstract
class
Option
{
{
...
@@ -12,39 +12,49 @@ abstract class Option
...
@@ -12,39 +12,49 @@ abstract class Option
'development'
=>
''
,
'development'
=>
''
,
'production'
=>
''
'production'
=>
''
];
];
const
AUTH_CACHE_EXPIRES_AT
=
30000
;
/** @var string App Key 应用标志 */
/** @var string App Key 应用标志 */
private
$app
_i
d
;
private
$app
I
d
;
/** @var string App Secret 应用密钥 */
/** @var string App Secret 应用密钥 */
private
$app
_s
ecret
;
private
$app
S
ecret
;
/** @var string SDK 的 Stage 环境 */
/** @var string SDK 的 Stage 环境 */
private
$stage
=
"development"
;
private
$stage
=
"development"
;
/** @var float 超时时间 */
/** @var float 超时时间 */
private
$time
_o
ut
=
3.0
;
private
$time
O
ut
=
3.0
;
/** @var bool 调试模式 */
/** @var bool 调试模式 */
private
$debug
=
false
;
private
$debug
=
false
;
/**
/**
* @return \Closure
* @return \Closure
*/
*/
abstract
public
function
getAuthMoudel
()
:
\Closure
;
abstract
public
function
authorization
()
:
\Closure
;
/**
* 错误处理策略
* @return HandlingPolicyInterface
*/
abstract
public
function
errorHandlingPolicy
()
:
HandlingPolicyInterface
;
/**
/**
* @param string $app
_i
d
* @param string $app
I
d
* @return Option
* @return Option
*/
*/
public
function
setAppId
(
string
$app
_i
d
)
:
self
public
function
setAppId
(
string
$app
I
d
)
:
self
{
{
$this
->
app
_id
=
$app_i
d
;
$this
->
app
Id
=
$appI
d
;
return
$this
;
return
$this
;
}
}
/**
/**
* @param string $app
_s
ecret
* @param string $app
S
ecret
* @return Option
* @return Option
*/
*/
public
function
setAppSecret
(
string
$app
_s
ecret
)
:
self
public
function
setAppSecret
(
string
$app
S
ecret
)
:
self
{
{
$this
->
app
_secret
=
$app_s
ecret
;
$this
->
app
Secret
=
$appS
ecret
;
return
$this
;
return
$this
;
}
}
...
@@ -59,12 +69,12 @@ abstract class Option
...
@@ -59,12 +69,12 @@ abstract class Option
}
}
/**
/**
* @param float $time
_o
ut
* @param float $time
O
ut
* @return Option
* @return Option
*/
*/
public
function
setTimeout
(
float
$time
_o
ut
)
:
self
public
function
setTimeout
(
float
$time
O
ut
)
:
self
{
{
$this
->
time
_out
=
$time_o
ut
;
$this
->
time
Out
=
$timeO
ut
;
return
$this
;
return
$this
;
}
}
...
@@ -83,7 +93,7 @@ abstract class Option
...
@@ -83,7 +93,7 @@ abstract class Option
*/
*/
public
function
getAppSecret
()
:
string
public
function
getAppSecret
()
:
string
{
{
return
$this
->
app
_s
ecret
;
return
$this
->
app
S
ecret
;
}
}
/**
/**
...
@@ -95,6 +105,15 @@ abstract class Option
...
@@ -95,6 +105,15 @@ abstract class Option
return
static
::
ENDPONIT_HOSTS
[
$this
->
getStage
()]
??
""
;
return
static
::
ENDPONIT_HOSTS
[
$this
->
getStage
()]
??
""
;
}
}
/**
* @return int
*/
public
function
getAuthExpires
()
:
int
{
return
static
::
AUTH_CACHE_EXPIRES_AT
??
30000
;
}
/**
/**
* @return string
* @return string
*/
*/
...
@@ -108,7 +127,7 @@ abstract class Option
...
@@ -108,7 +127,7 @@ abstract class Option
*/
*/
public
function
getTimeOut
()
:
float
public
function
getTimeOut
()
:
float
{
{
return
$this
->
time
_o
ut
;
return
$this
->
time
O
ut
;
}
}
/**
/**
...
@@ -116,7 +135,7 @@ abstract class Option
...
@@ -116,7 +135,7 @@ abstract class Option
*/
*/
public
function
getAppId
()
:
string
public
function
getAppId
()
:
string
{
{
return
$this
->
app
_i
d
;
return
$this
->
app
I
d
;
}
}
/**
/**
...
...
src/Policy/DefaultErrorHandlingPolicy.php
0 → 100644
View file @
66e213ae
<?php
namespace
Jiwei\EasyHttpSdk\Policy
;
use
Jiwei\EasyHttpSdk\Exception\GuiltyResultException
;
use
Jiwei\EasyHttpSdk\Exception\UnknowResultException
;
use
Psr\Http\Message\ResponseInterface
;
class
DefaultErrorHandlingPolicy
implements
HandlingPolicyInterface
{
/**
* @param ResponseInterface $response
* @return array<string, mixed>
*/
public
function
process
(
ResponseInterface
$response
)
:
array
{
if
(
$response
->
getStatusCode
()
==
304
)
{
return
[];
}
$responseInfo
=
$response
->
getBody
()
->
getContents
();
$rpcResult
=
json_decode
(
$responseInfo
,
true
);
if
(
json_last_error
())
{
throw
new
UnknowResultException
(
"Content Format error."
,
$responseInfo
);
}
if
(
!
empty
(
$rpcResult
[
'name'
])
&&
$response
->
getStatusCode
()
>=
400
)
{
$errorMessage
=
sprintf
(
"%s : %s"
,
$rpcResult
[
'name'
],
$rpcResult
[
'message'
]
??
""
);
throw
new
GuiltyResultException
(
$errorMessage
,
$rpcResult
);
}
return
$rpcResult
;
}
}
src/Policy/HandlingPolicyInterface.php
0 → 100644
View file @
66e213ae
<?php
namespace
Jiwei\EasyHttpSdk\Policy
;
use
Psr\Http\Message\ResponseInterface
;
interface
HandlingPolicyInterface
{
/**
* @param ResponseInterface $response
* @return array<string, mixed>
*/
public
function
process
(
ResponseInterface
$response
)
:
array
;
}
src/XXXSDKOption.php
View file @
66e213ae
<?php
<?php
declare
(
strict_types
=
1
);
namespace
Jiwei\EasyHttpSdk
;
namespace
Jiwei\EasyHttpSdk
;
use
GuzzleHttp\Psr7\Request
;
use
GuzzleHttp\Psr7\Request
;
use
Jiwei\EasyHttpSdk\Policy\DefaultErrorHandlingPolicy
;
use
Jiwei\EasyHttpSdk\Policy\HandlingPolicyInterface
;
class
XXXSDKOption
extends
Option
class
XXXSDKOption
extends
Option
{
{
private
const
AUTH_API_ROUTE
=
"/api/access/token"
;
private
const
AUTH_API_ROUTE
=
"/api/access/token"
;
const
AUTH_CACHE_EXPIRES_AT
=
30000
;
const
ENDPONIT_HOSTS
=
[
const
ENDPONIT_HOSTS
=
[
'local'
=>
'localhost:8080'
,
'local'
=>
'localhost:8080'
,
'development'
=>
'localhost:8080'
,
'development'
=>
'localhost:8080'
,
'production'
=>
'localhost:8080'
'production'
=>
'localhost:8080'
];
];
public
function
getAuthMoudel
()
:
\Closure
/**
* 鉴权模式
*
* @return \Closure
*/
public
function
authorization
()
:
\Closure
{
{
return
function
(
string
$appId
,
string
$appSecret
)
:
Request
{
return
function
(
string
$appId
,
string
$appSecret
)
:
Request
{
return
new
Request
(
'post'
,
self
::
AUTH_API_ROUTE
,
[
return
new
Request
(
'post'
,
self
::
AUTH_API_ROUTE
,
[
...
@@ -24,6 +34,14 @@ class XXXSDKOption extends Option
...
@@ -24,6 +34,14 @@ class XXXSDKOption extends Option
])
])
]);
]);
};
};
}
/**
* 错误处理策略
* @return HandlingPolicyInterface
*/
public
function
errorHandlingPolicy
()
:
HandlingPolicyInterface
{
return
new
DefaultErrorHandlingPolicy
();
}
}
}
}
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