Skip to content

Commit

Permalink
change(internal): 错误代码 const 命名规则统一 (#902)
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda authored Dec 22, 2023
1 parent 381591c commit 9b25509
Show file tree
Hide file tree
Showing 132 changed files with 217 additions and 220 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
- change(internal): 支付宝 shortcut 从 plugin 文件夹独立出来(#895)
- change(internal): DirectionInterface 方法由 `parse` 改为 `guide`(#896)
- change: 查询API方法由 `find` 改为 `query`,同时参数只支持 array(#897)
- change: cancel 的 API 参数只支持 array,不再支持 string(#890)
- change: close 的 API 参数只支持 array,不再支持 string(#891)
- change: cancel 的 API 参数只支持 array,不再支持 string(#900)
- change: close 的 API 参数只支持 array,不再支持 string(#901)
- change(internal): 错误代码 const 命名规则统一(#902)

## v3.5.3

Expand Down
2 changes: 1 addition & 1 deletion src/Direction/CollectionDirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CollectionDirection implements DirectionInterface
public function guide(PackerInterface $packer, ?ResponseInterface $response): Collection
{
if (is_null($response)) {
throw new InvalidResponseException(Exception::RESPONSE_NONE);
throw new InvalidResponseException(Exception::RESPONSE_EMPTY);
}

$body = (string) $response->getBody();
Expand Down
2 changes: 1 addition & 1 deletion src/Direction/OriginResponseDirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public function guide(PackerInterface $packer, ?ResponseInterface $response): ?R
return $response;
}

throw new InvalidResponseException(Exception::INVALID_RESPONSE_CODE);
throw new InvalidResponseException(Exception::RESPONSE_CODE_WRONG);
}
}
2 changes: 1 addition & 1 deletion src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public static function __callStatic(string $method, array $args): void
return;
}

throw new InvalidConfigException(Exception\Exception::EVENT_CONFIG_ERROR);
throw new InvalidConfigException(Exception\Exception::EVENT_CONFIG_INVALID);
}
}
46 changes: 21 additions & 25 deletions src/Exception/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ class Exception extends \Exception
*/
public const SERVICE_ERROR = 2000;

public const SERVICE_NOT_FOUND_ERROR = 2001;
public const SERVICE_NOT_FOUND = 2001;

/*
* 关于配置.
*/
public const CONFIG_ERROR = 3000;

public const INVALID_DIRECTION = 3001;
public const DIRECTION_INVALID = 3001;

public const ALIPAY_CONFIG_ERROR = 3002;
public const ALIPAY_CONFIG_INVALID = 3002;

public const LOGGER_CONFIG_ERROR = 3003;
public const LOGGER_CONFIG_INVALID = 3003;

public const HTTP_CLIENT_CONFIG_ERROR = 3004;
public const HTTP_CLIENT_INVALID = 3004;

public const EVENT_CONFIG_ERROR = 3005;
public const EVENT_CONFIG_INVALID = 3005;

public const WECHAT_CONFIG_ERROR = 3006;
public const WECHAT_CONFIG_INVALID = 3006;

public const UNIPAY_CONFIG_ERROR = 3007;
public const UNIPAY_CONFIG_INVALID = 3007;

public const INVALID_PACKER = 3008;
public const PACKER_INVALID = 3008;

/*
* 关于参数.
Expand All @@ -54,23 +54,19 @@ class Exception extends \Exception

public const SHORTCUT_NOT_FOUND = 4001;

public const PLUGIN_ERROR = 4002;
public const PLUGIN_INCOMPATIBLE = 4002;

public const SHORTCUT_MULTI_ACTION_ERROR = 4003;
public const SHORTCUT_MULTI_ACTION_INVALID = 4003;

public const METHOD_NOT_SUPPORTED = 4004;

public const REQUEST_NULL_ERROR = 4005;
public const REQUEST_EMPTY = 4005;

public const MISSING_NECESSARY_PARAMS = 4006;
public const NECESSARY_PARAMS_MISSING = 4006;

public const NOT_IN_SERVICE_MODE = 4007;
public const CURRENT_PLUGIN_ONLY_SUPPORT_SERVICE_MODE = 4007;

public const WECHAT_SERIAL_NO_NOT_FOUND = 4008;

public const UNIPAY_FIND_STRING_NOT_SUPPORTED = 4009;

public const UNIPAY_CANCEL_STRING_NOT_SUPPORTED = 4010;
public const WECHAT_SERIAL_NOT_FOUND = 4008;

/**
* 关于api.
Expand All @@ -81,19 +77,19 @@ class Exception extends \Exception

public const UNPACK_RESPONSE_ERROR = 5002;

public const INVALID_RESPONSE_SIGN = 5003;
public const SIGN_INVALID = 5003;

public const INVALID_RESPONSE_CODE = 5004;
public const RESPONSE_CODE_WRONG = 5004;

public const RESPONSE_MISSING_NECESSARY_PARAMS = 5005;

public const RESPONSE_NONE = 5006;
public const RESPONSE_EMPTY = 5006;

public const INVALID_CIPHERTEXT_PARAMS = 5007;
public const CIPHERTEXT_PARAMS_INVALID = 5007;

public const INVALID_REQUEST_ENCRYPTED_DATA = 5008;
public const REQUEST_ENCRYPTED_DATA_INVALID = 5008;

public const INVALID_REQUEST_ENCRYPTED_METHOD = 5009;
public const REQUEST_ENCRYPTED_METHOD_INVALID = 5009;

public mixed $extra;

Expand Down
2 changes: 1 addition & 1 deletion src/Exception/ServiceNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class ServiceNotFoundException extends Exception implements NotFoundExceptionInterface
{
public function __construct(string $message = 'Service Not Found', int $code = self::SERVICE_NOT_FOUND_ERROR, mixed $extra = null, Throwable $previous = null)
public function __construct(string $message = 'Service Not Found', int $code = self::SERVICE_NOT_FOUND, mixed $extra = null, Throwable $previous = null)
{
parent::__construct($message, $code, $extra, $previous);
}
Expand Down
28 changes: 14 additions & 14 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function get_direction(mixed $direction): DirectionInterface
}

if (!$direction instanceof DirectionInterface) {
throw new InvalidConfigException(Exception::INVALID_DIRECTION);
throw new InvalidConfigException(Exception::DIRECTION_INVALID);
}

return $direction;
Expand Down Expand Up @@ -90,7 +90,7 @@ function verify_alipay_sign(array $params, string $contents, string $sign): void
$public = get_alipay_config($params)['alipay_public_cert_path'] ?? null;

if (empty($public)) {
throw new InvalidConfigException(Exception::ALIPAY_CONFIG_ERROR, 'Missing Alipay Config -- [alipay_public_cert_path]');
throw new InvalidConfigException(Exception::ALIPAY_CONFIG_INVALID, 'Missing Alipay Config -- [alipay_public_cert_path]');
}

$result = 1 === openssl_verify(
Expand All @@ -101,7 +101,7 @@ function verify_alipay_sign(array $params, string $contents, string $sign): void
);

if (!$result) {
throw new InvalidResponseException(Exception::INVALID_RESPONSE_SIGN, 'Verify Alipay Response Sign Failed', func_get_args());
throw new InvalidResponseException(Exception::SIGN_INVALID, 'Verify Alipay Response Sign Failed', func_get_args());
}
}

Expand Down Expand Up @@ -137,7 +137,7 @@ function get_wechat_sign(array $params, string $contents): string
$privateKey = get_wechat_config($params)['mch_secret_cert'] ?? null;

if (empty($privateKey)) {
throw new InvalidConfigException(Exception::WECHAT_CONFIG_ERROR, 'Missing Wechat Config -- [mch_secret_cert]');
throw new InvalidConfigException(Exception::WECHAT_CONFIG_INVALID, 'Missing Wechat Config -- [mch_secret_cert]');
}

$privateKey = get_private_cert($privateKey);
Expand All @@ -157,7 +157,7 @@ function get_wechat_sign_v2(array $params, array $payload, bool $upper = true):
$key = get_wechat_config($params)['mch_secret_key_v2'] ?? null;

if (empty($key)) {
throw new InvalidConfigException(Exception::WECHAT_CONFIG_ERROR, 'Missing Wechat Config -- [mch_secret_key_v2]');
throw new InvalidConfigException(Exception::WECHAT_CONFIG_INVALID, 'Missing Wechat Config -- [mch_secret_key_v2]');
}

ksort($payload);
Expand Down Expand Up @@ -196,7 +196,7 @@ function verify_wechat_sign(ResponseInterface|ServerRequestInterface $message, a
$public = get_wechat_config($params)['wechat_public_cert_path'][$wechatSerial] ?? null;

if (empty($sign)) {
throw new InvalidResponseException(Exception::INVALID_RESPONSE_SIGN, $body, ['headers' => $message->getHeaders(), 'body' => $body]);
throw new InvalidResponseException(Exception::SIGN_INVALID, $body, ['headers' => $message->getHeaders(), 'body' => $body]);
}

$public = get_public_cert(
Expand All @@ -211,7 +211,7 @@ function verify_wechat_sign(ResponseInterface|ServerRequestInterface $message, a
);

if (!$result) {
throw new InvalidResponseException(Exception::INVALID_RESPONSE_SIGN, '', ['headers' => $message->getHeaders(), 'body' => $body]);
throw new InvalidResponseException(Exception::SIGN_INVALID, '', ['headers' => $message->getHeaders(), 'body' => $body]);
}
}

Expand Down Expand Up @@ -250,7 +250,7 @@ function reload_wechat_public_certs(array $params, ?string $serialNo = null): st
);

if (!is_null($serialNo) && empty($certs[$serialNo])) {
throw new InvalidConfigException(Exception::WECHAT_CONFIG_ERROR, 'Get Wechat Public Cert Error');
throw new InvalidConfigException(Exception::WECHAT_CONFIG_INVALID, 'Get Wechat Public Cert Error');
}

return $certs[$serialNo] ?? '';
Expand Down Expand Up @@ -292,16 +292,16 @@ function decrypt_wechat_resource(array $resource, array $params): array
$secret = get_wechat_config($params)['mch_secret_key'] ?? null;

if (strlen($ciphertext) <= Wechat::AUTH_TAG_LENGTH_BYTE) {
throw new InvalidResponseException(Exception::INVALID_CIPHERTEXT_PARAMS);
throw new InvalidResponseException(Exception::CIPHERTEXT_PARAMS_INVALID);
}

if (is_null($secret) || Wechat::MCH_SECRET_KEY_LENGTH_BYTE != strlen($secret)) {
throw new InvalidConfigException(Exception::WECHAT_CONFIG_ERROR, 'Missing Wechat Config -- [mch_secret_key]');
throw new InvalidConfigException(Exception::WECHAT_CONFIG_INVALID, 'Missing Wechat Config -- [mch_secret_key]');
}

$resource['ciphertext'] = match ($resource['algorithm'] ?? '') {
'AEAD_AES_256_GCM' => decrypt_wechat_resource_aes_256_gcm($ciphertext, $secret, $resource['nonce'] ?? '', $resource['associated_data'] ?? ''),
default => throw new InvalidResponseException(Exception::INVALID_REQUEST_ENCRYPTED_METHOD),
default => throw new InvalidResponseException(Exception::REQUEST_ENCRYPTED_METHOD_INVALID),
};

return $resource;
Expand All @@ -326,7 +326,7 @@ function decrypt_wechat_resource_aes_256_gcm(string $ciphertext, string $secret,
$decrypted = json_decode(strval($decrypted), true);

if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidResponseException(Exception::INVALID_REQUEST_ENCRYPTED_DATA);
throw new InvalidResponseException(Exception::REQUEST_ENCRYPTED_DATA_INVALID);
}
}

Expand Down Expand Up @@ -354,7 +354,7 @@ function verify_unipay_sign(array $params, string $contents, string $sign): void
{
if (empty($params['signPubKeyCert'])
&& empty($public = get_unipay_config($params)['unipay_public_cert_path'] ?? null)) {
throw new InvalidConfigException(Exception::UNIPAY_CONFIG_ERROR, 'Missing Unipay Config -- [unipay_public_cert_path]');
throw new InvalidConfigException(Exception::UNIPAY_CONFIG_INVALID, 'Missing Unipay Config -- [unipay_public_cert_path]');
}

$result = 1 === openssl_verify(
Expand All @@ -365,6 +365,6 @@ function verify_unipay_sign(array $params, string $contents, string $sign): void
);

if (!$result) {
throw new InvalidResponseException(Exception::INVALID_RESPONSE_SIGN, 'Verify Unipay Response Sign Failed', func_get_args());
throw new InvalidResponseException(Exception::SIGN_INVALID, 'Verify Unipay Response Sign Failed', func_get_args());
}
}
2 changes: 1 addition & 1 deletion src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public static function __callStatic(string $method, array $args): void
return;
}

throw new InvalidConfigException(Exception\Exception::LOGGER_CONFIG_ERROR);
throw new InvalidConfigException(Exception\Exception::LOGGER_CONFIG_INVALID);
}
}
2 changes: 1 addition & 1 deletion src/Plugin/Alipay/AddSignaturePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function getPrivateKey(array $params): string
$privateKey = get_alipay_config($params)['app_secret_cert'] ?? null;

if (is_null($privateKey)) {
throw new InvalidConfigException(Exception::ALIPAY_CONFIG_ERROR, 'Missing Alipay Config -- [app_secret_cert]');
throw new InvalidConfigException(Exception::ALIPAY_CONFIG_INVALID, 'Missing Alipay Config -- [app_secret_cert]');
}

return get_private_cert($privateKey);
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Alipay/CallbackPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function assembly(Rocket $rocket, Closure $next): Rocket
$sign = $rocket->getParams()['sign'] ?? false;

if (!$sign) {
throw new InvalidResponseException(Exception::INVALID_RESPONSE_SIGN, 'Callback Empty Sign', $rocket->getParams());
throw new InvalidResponseException(Exception::SIGN_INVALID, 'Callback Empty Sign', $rocket->getParams());
}

verify_alipay_sign($rocket->getParams(), $this->getSignContent($rocket->getPayload()), $sign);
Expand Down
8 changes: 4 additions & 4 deletions src/Plugin/Alipay/StartPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ protected function getAppCertSn(string $tenant, array $config): string
$path = $config['app_public_cert_path'] ?? null;

if (is_null($path)) {
throw new InvalidConfigException(Exception::ALIPAY_CONFIG_ERROR, 'Missing Alipay Config -- [app_public_cert_path]');
throw new InvalidConfigException(Exception::ALIPAY_CONFIG_INVALID, 'Missing Alipay Config -- [app_public_cert_path]');
}

$ssl = openssl_x509_parse(get_public_cert($path));

if (false === $ssl) {
throw new InvalidConfigException(Exception::ALIPAY_CONFIG_ERROR, 'Parse `app_public_cert_path` Error');
throw new InvalidConfigException(Exception::ALIPAY_CONFIG_INVALID, 'Parse `app_public_cert_path` Error');
}

$result = $this->getCertSn($ssl['issuer'] ?? [], $ssl['serialNumber'] ?? '');
Expand All @@ -136,7 +136,7 @@ protected function getAlipayRootCertSn(string $tenant, array $config): string
$path = $config['alipay_root_cert_path'] ?? null;

if (is_null($path)) {
throw new InvalidConfigException(Exception::ALIPAY_CONFIG_ERROR, 'Missing Alipay Config -- [alipay_root_cert_path]');
throw new InvalidConfigException(Exception::ALIPAY_CONFIG_INVALID, 'Missing Alipay Config -- [alipay_root_cert_path]');
}

$sn = '';
Expand All @@ -150,7 +150,7 @@ protected function getAlipayRootCertSn(string $tenant, array $config): string
$ssl = openssl_x509_parse($cert.'-----END CERTIFICATE-----');

if (false === $ssl) {
throw new InvalidConfigException(Exception::ALIPAY_CONFIG_ERROR, 'Invalid alipay_root_cert');
throw new InvalidConfigException(Exception::ALIPAY_CONFIG_INVALID, 'Invalid alipay_root_cert');
}

$detail = $this->formatCert($ssl);
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Alipay/VerifySignaturePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function assembly(Rocket $rocket, Closure $next): Rocket
$result = $destination->except('_sign')->all();

if ('' === $sign || empty($result)) {
throw new InvalidResponseException(Exception::INVALID_RESPONSE_SIGN, 'Verify Alipay Response Sign Failed: sign is empty', $destination);
throw new InvalidResponseException(Exception::SIGN_INVALID, 'Verify Alipay Response Sign Failed: sign is empty', $destination);
}

verify_alipay_sign($rocket->getParams(), json_encode($result, JSON_UNESCAPED_UNICODE), $sign);
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/ParserPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function getPacker(Rocket $rocket): PackerInterface
$packer = is_string($packer) ? Pay::get($packer) : $packer;

if (!$packer instanceof PackerInterface) {
throw new InvalidConfigException(Exception::INVALID_PACKER);
throw new InvalidConfigException(Exception::PACKER_INVALID);
}

return $packer;
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Unipay/CallbackPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function assembly(Rocket $rocket, Closure $next): Rocket
$signature = $params['signature'] ?? false;

if (!$signature) {
throw new InvalidResponseException(Exception::INVALID_RESPONSE_SIGN, '', $params);
throw new InvalidResponseException(Exception::SIGN_INVALID, '', $params);
}

verify_unipay_sign($params, $rocket->getPayload()->sortKeys()->toString(), $signature);
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Unipay/Shortcut/CancelShortcut.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getPlugins(array $params): array
return $this->{$typeMethod}();
}

throw new InvalidParamsException(Exception::SHORTCUT_MULTI_ACTION_ERROR, "Cancel action [{$typeMethod}] not supported");
throw new InvalidParamsException(Exception::SHORTCUT_MULTI_ACTION_INVALID, "Cancel action [{$typeMethod}] not supported");
}

protected function defaultPlugins(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Unipay/Shortcut/PosShortcut.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function getPlugins(array $params): array
return $this->{$typeMethod}();
}

throw new InvalidParamsException(Exception::SHORTCUT_MULTI_ACTION_ERROR, "Pos action [{$typeMethod}] not supported");
throw new InvalidParamsException(Exception::SHORTCUT_MULTI_ACTION_INVALID, "Pos action [{$typeMethod}] not supported");
}

protected function defaultPlugins(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Unipay/Shortcut/QueryShortcut.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getPlugins(array $params): array
return $this->{$typeMethod}();
}

throw new InvalidParamsException(Exception::SHORTCUT_MULTI_ACTION_ERROR, "Query action [{$typeMethod}] not supported");
throw new InvalidParamsException(Exception::SHORTCUT_MULTI_ACTION_INVALID, "Query action [{$typeMethod}] not supported");
}

protected function defaultPlugins(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Unipay/Shortcut/RefundShortcut.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getPlugins(array $params): array
return $this->{$typeMethod}();
}

throw new InvalidParamsException(Exception::SHORTCUT_MULTI_ACTION_ERROR, "Refund action [{$typeMethod}] not supported");
throw new InvalidParamsException(Exception::SHORTCUT_MULTI_ACTION_INVALID, "Refund action [{$typeMethod}] not supported");
}

protected function defaultPlugins(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Unipay/Shortcut/ScanShortcut.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getPlugins(array $params): array
return $this->{$typeMethod}();
}

throw new InvalidParamsException(Exception::SHORTCUT_MULTI_ACTION_ERROR, "Scan action [{$typeMethod}] not supported");
throw new InvalidParamsException(Exception::SHORTCUT_MULTI_ACTION_INVALID, "Scan action [{$typeMethod}] not supported");
}

protected function defaultPlugins(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Wechat/CallbackPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function formatRequestAndParams(Rocket $rocket): void
$request = $rocket->getParams()['request'] ?? null;

if (!$request instanceof ServerRequestInterface) {
throw new InvalidParamsException(Exception::REQUEST_NULL_ERROR);
throw new InvalidParamsException(Exception::REQUEST_EMPTY);
}

$rocket->setDestination(clone $request)
Expand Down
Loading

0 comments on commit 9b25509

Please sign in to comment.