@@ -4,41 +4,41 @@ |
||
| 4 | 4 | |
| 5 | 5 | final class UnexpectedJsonTextResponseException extends InvalidResponseException |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @var string the unexpected JSON Text of the API response |
|
| 9 | - */ |
|
| 10 | - private $jsonText; |
|
| 11 | - |
|
| 12 | - /** |
|
| 13 | - * utility method to shorten a string (e.g. to create shorter messages) |
|
| 14 | - * |
|
| 15 | - * @param string $buffer |
|
| 16 | - * @param int $size |
|
| 17 | - * @return string ASCII, printable with other characters escaped (C-slashes) |
|
| 18 | - */ |
|
| 19 | - public static function shorten(string $buffer, int $size = 32): string |
|
| 20 | - { |
|
| 21 | - if ($size >= $len = strlen($buffer)) { |
|
| 22 | - $buffer = addcslashes($buffer, "\0..\37\42\134\177..\377"); |
|
| 23 | - return "\"$buffer\""; |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - $buffer = substr($buffer, 0, max(0, $size - 8)); |
|
| 27 | - $buffer = addcslashes($buffer, "\0..\37\42\134\177..\377"); |
|
| 28 | - |
|
| 29 | - return "($len) \"$buffer ..."; |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - public static function create(string $message, string $jsonText, \Throwable $previous = null): UnexpectedJsonTextResponseException |
|
| 33 | - { |
|
| 34 | - $exception = new self($message, 500, $previous); |
|
| 35 | - $exception->jsonText = $jsonText; |
|
| 36 | - |
|
| 37 | - return $exception; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - public function getJsonText(): string |
|
| 41 | - { |
|
| 42 | - return $this->jsonText; |
|
| 43 | - } |
|
| 7 | + /** |
|
| 8 | + * @var string the unexpected JSON Text of the API response |
|
| 9 | + */ |
|
| 10 | + private $jsonText; |
|
| 11 | + |
|
| 12 | + /** |
|
| 13 | + * utility method to shorten a string (e.g. to create shorter messages) |
|
| 14 | + * |
|
| 15 | + * @param string $buffer |
|
| 16 | + * @param int $size |
|
| 17 | + * @return string ASCII, printable with other characters escaped (C-slashes) |
|
| 18 | + */ |
|
| 19 | + public static function shorten(string $buffer, int $size = 32): string |
|
| 20 | + { |
|
| 21 | + if ($size >= $len = strlen($buffer)) { |
|
| 22 | + $buffer = addcslashes($buffer, "\0..\37\42\134\177..\377"); |
|
| 23 | + return "\"$buffer\""; |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + $buffer = substr($buffer, 0, max(0, $size - 8)); |
|
| 27 | + $buffer = addcslashes($buffer, "\0..\37\42\134\177..\377"); |
|
| 28 | + |
|
| 29 | + return "($len) \"$buffer ..."; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + public static function create(string $message, string $jsonText, \Throwable $previous = null): UnexpectedJsonTextResponseException |
|
| 33 | + { |
|
| 34 | + $exception = new self($message, 500, $previous); |
|
| 35 | + $exception->jsonText = $jsonText; |
|
| 36 | + |
|
| 37 | + return $exception; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + public function getJsonText(): string |
|
| 41 | + { |
|
| 42 | + return $this->jsonText; |
|
| 43 | + } |
|
| 44 | 44 | } |
@@ -12,154 +12,154 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | final class ConnectionException extends Exception |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * HTTP status code of the response |
|
| 17 | - */ |
|
| 18 | - private ?int $responseCode = null; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * HTTP raw response (if any) |
|
| 22 | - */ |
|
| 23 | - private ?string $responseBodyRaw = null; |
|
| 24 | - |
|
| 25 | - private const HTTP_STATUS = [ |
|
| 26 | - 200 => 'OK', |
|
| 27 | - 201 => 'Created', |
|
| 28 | - 202 => 'Accepted', |
|
| 29 | - 301 => 'Moved Permanently', |
|
| 30 | - 400 => 'Bad Request', |
|
| 31 | - 401 => 'Unauthorized', |
|
| 32 | - 403 => 'Forbidden', |
|
| 33 | - 404 => 'Not Found', |
|
| 34 | - 409 => 'Conflict', |
|
| 35 | - 429 => 'Too Many Requests', |
|
| 36 | - 440 => 'REPO_PASSWD_REQUIRED', |
|
| 37 | - 441 => 'REPO_PASSWD_MAGIC_REQUIRED', |
|
| 38 | - 500 => 'Internal Server Error', |
|
| 39 | - 520 => 'OPERATION_FAILED', |
|
| 40 | - ]; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @param int $code HTTP status code, e.g. curl_getinfo($curl)['http_code'] |
|
| 44 | - * @param bool|string $curlResult return value from curl_exec(); |
|
| 45 | - * @throws ConnectionException |
|
| 46 | - * @return no-return |
|
| 47 | - */ |
|
| 48 | - public static function throwCurlResult(int $code, string|bool $curlResult): never |
|
| 49 | - { |
|
| 50 | - $exception = new self(self::reasonPhrase($code), $code); |
|
| 51 | - $exception->responseCode = $code; |
|
| 52 | - $exception->responseBodyRaw = is_string($curlResult) ? $curlResult : null; |
|
| 53 | - |
|
| 54 | - throw $exception; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - private static function reasonPhrase(int $code): string |
|
| 58 | - { |
|
| 59 | - return sprintf('%s %s', $code, self::HTTP_STATUS[$code] ?? "UNKNOWN_PHRASE"); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) |
|
| 63 | - { |
|
| 64 | - // trigger E_USER_NOTICE if code is not known |
|
| 65 | - $isHttpCode = 100 <= $code && $code < 600; |
|
| 66 | - $isKnownHttpCode = $isHttpCode && isset(self::HTTP_STATUS[$code]); |
|
| 67 | - $isKnownCode = $code === -1 || $isKnownHttpCode; |
|
| 68 | - $this->responseCode = $isHttpCode ? $code : null; |
|
| 69 | - $isKnownCode || trigger_error(sprintf("%s: Unknown code: %s (%s)", __CLASS__, $code, gettype($code)), E_USER_NOTICE); |
|
| 70 | - |
|
| 71 | - parent::__construct($message, $code, $previous); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - public function getStatusCode(): ?int |
|
| 75 | - { |
|
| 76 | - return $this->responseCode; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @param int $code |
|
| 81 | - * @throws ConnectionException |
|
| 82 | - * @return void |
|
| 83 | - */ |
|
| 84 | - public function assertStatusCode(int $code): void |
|
| 85 | - { |
|
| 86 | - if ($this->responseCode !== $code) { |
|
| 87 | - throw $this; |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * The raw response body |
|
| 93 | - * |
|
| 94 | - * @return string|null |
|
| 95 | - */ |
|
| 96 | - public function getRawResponse(): ?string |
|
| 97 | - { |
|
| 98 | - return $this->responseBodyRaw; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Response body parsed as JSON Object |
|
| 103 | - * |
|
| 104 | - * @return null|object - null either if the parsed response is NULL or if it can't be parsed as object |
|
| 105 | - */ |
|
| 106 | - public function tryParsedResponse(): ?object |
|
| 107 | - { |
|
| 108 | - /** |
|
| 109 | - * @noinspection JsonEncodingApiUsageInspection |
|
| 110 | - * @noinspection RedundantSuppression |
|
| 111 | - */ |
|
| 112 | - $result = json_decode((string)$this->responseBodyRaw, false); |
|
| 113 | - return is_object($result) ? $result : null; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - public function getReasonPhrase(): ?string |
|
| 117 | - { |
|
| 118 | - $code = $this->responseCode; |
|
| 119 | - |
|
| 120 | - if (!is_int($code)) { |
|
| 121 | - return null; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return self::HTTP_STATUS[$code] ?? null; |
|
| 125 | - |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * A seafile JSON response may contain error information, try to get them. |
|
| 130 | - * |
|
| 131 | - * It is not fool-proof or overly complete but often more informative than just looking at JSON response dumps. |
|
| 132 | - * |
|
| 133 | - * @return null|array|string[] messages, null if n/a otherwise array of messages (which can not be empty) |
|
| 134 | - */ |
|
| 135 | - public function tryApiErrorMessages(): ?array |
|
| 136 | - { |
|
| 137 | - $response = $this->tryParsedResponse(); |
|
| 138 | - if (null === $response) { |
|
| 139 | - return null; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - $buffer = []; |
|
| 143 | - |
|
| 144 | - // {"error_msg": ... } |
|
| 145 | - if (isset($response->error_msg) && is_string($response->error_msg)) { |
|
| 146 | - $buffer[] = $response->error_msg; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - // {"detail": "Invalid token header. No credentials provided."} |
|
| 150 | - if (isset($response->detail) && is_string($response->detail)) { |
|
| 151 | - $buffer[] = $response->detail; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - // {"non_field_errors": [ "string...", ...]} |
|
| 155 | - if (isset($response->non_field_errors) && is_array($response->non_field_errors)) { |
|
| 156 | - foreach ($response->non_field_errors as $message) { |
|
| 157 | - if (is_string($message)) { |
|
| 158 | - $buffer[] = $message; |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - return empty($buffer) ? null : $buffer; |
|
| 164 | - } |
|
| 15 | + /** |
|
| 16 | + * HTTP status code of the response |
|
| 17 | + */ |
|
| 18 | + private ?int $responseCode = null; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * HTTP raw response (if any) |
|
| 22 | + */ |
|
| 23 | + private ?string $responseBodyRaw = null; |
|
| 24 | + |
|
| 25 | + private const HTTP_STATUS = [ |
|
| 26 | + 200 => 'OK', |
|
| 27 | + 201 => 'Created', |
|
| 28 | + 202 => 'Accepted', |
|
| 29 | + 301 => 'Moved Permanently', |
|
| 30 | + 400 => 'Bad Request', |
|
| 31 | + 401 => 'Unauthorized', |
|
| 32 | + 403 => 'Forbidden', |
|
| 33 | + 404 => 'Not Found', |
|
| 34 | + 409 => 'Conflict', |
|
| 35 | + 429 => 'Too Many Requests', |
|
| 36 | + 440 => 'REPO_PASSWD_REQUIRED', |
|
| 37 | + 441 => 'REPO_PASSWD_MAGIC_REQUIRED', |
|
| 38 | + 500 => 'Internal Server Error', |
|
| 39 | + 520 => 'OPERATION_FAILED', |
|
| 40 | + ]; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @param int $code HTTP status code, e.g. curl_getinfo($curl)['http_code'] |
|
| 44 | + * @param bool|string $curlResult return value from curl_exec(); |
|
| 45 | + * @throws ConnectionException |
|
| 46 | + * @return no-return |
|
| 47 | + */ |
|
| 48 | + public static function throwCurlResult(int $code, string|bool $curlResult): never |
|
| 49 | + { |
|
| 50 | + $exception = new self(self::reasonPhrase($code), $code); |
|
| 51 | + $exception->responseCode = $code; |
|
| 52 | + $exception->responseBodyRaw = is_string($curlResult) ? $curlResult : null; |
|
| 53 | + |
|
| 54 | + throw $exception; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + private static function reasonPhrase(int $code): string |
|
| 58 | + { |
|
| 59 | + return sprintf('%s %s', $code, self::HTTP_STATUS[$code] ?? "UNKNOWN_PHRASE"); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) |
|
| 63 | + { |
|
| 64 | + // trigger E_USER_NOTICE if code is not known |
|
| 65 | + $isHttpCode = 100 <= $code && $code < 600; |
|
| 66 | + $isKnownHttpCode = $isHttpCode && isset(self::HTTP_STATUS[$code]); |
|
| 67 | + $isKnownCode = $code === -1 || $isKnownHttpCode; |
|
| 68 | + $this->responseCode = $isHttpCode ? $code : null; |
|
| 69 | + $isKnownCode || trigger_error(sprintf("%s: Unknown code: %s (%s)", __CLASS__, $code, gettype($code)), E_USER_NOTICE); |
|
| 70 | + |
|
| 71 | + parent::__construct($message, $code, $previous); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + public function getStatusCode(): ?int |
|
| 75 | + { |
|
| 76 | + return $this->responseCode; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @param int $code |
|
| 81 | + * @throws ConnectionException |
|
| 82 | + * @return void |
|
| 83 | + */ |
|
| 84 | + public function assertStatusCode(int $code): void |
|
| 85 | + { |
|
| 86 | + if ($this->responseCode !== $code) { |
|
| 87 | + throw $this; |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * The raw response body |
|
| 93 | + * |
|
| 94 | + * @return string|null |
|
| 95 | + */ |
|
| 96 | + public function getRawResponse(): ?string |
|
| 97 | + { |
|
| 98 | + return $this->responseBodyRaw; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Response body parsed as JSON Object |
|
| 103 | + * |
|
| 104 | + * @return null|object - null either if the parsed response is NULL or if it can't be parsed as object |
|
| 105 | + */ |
|
| 106 | + public function tryParsedResponse(): ?object |
|
| 107 | + { |
|
| 108 | + /** |
|
| 109 | + * @noinspection JsonEncodingApiUsageInspection |
|
| 110 | + * @noinspection RedundantSuppression |
|
| 111 | + */ |
|
| 112 | + $result = json_decode((string)$this->responseBodyRaw, false); |
|
| 113 | + return is_object($result) ? $result : null; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + public function getReasonPhrase(): ?string |
|
| 117 | + { |
|
| 118 | + $code = $this->responseCode; |
|
| 119 | + |
|
| 120 | + if (!is_int($code)) { |
|
| 121 | + return null; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return self::HTTP_STATUS[$code] ?? null; |
|
| 125 | + |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * A seafile JSON response may contain error information, try to get them. |
|
| 130 | + * |
|
| 131 | + * It is not fool-proof or overly complete but often more informative than just looking at JSON response dumps. |
|
| 132 | + * |
|
| 133 | + * @return null|array|string[] messages, null if n/a otherwise array of messages (which can not be empty) |
|
| 134 | + */ |
|
| 135 | + public function tryApiErrorMessages(): ?array |
|
| 136 | + { |
|
| 137 | + $response = $this->tryParsedResponse(); |
|
| 138 | + if (null === $response) { |
|
| 139 | + return null; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + $buffer = []; |
|
| 143 | + |
|
| 144 | + // {"error_msg": ... } |
|
| 145 | + if (isset($response->error_msg) && is_string($response->error_msg)) { |
|
| 146 | + $buffer[] = $response->error_msg; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + // {"detail": "Invalid token header. No credentials provided."} |
|
| 150 | + if (isset($response->detail) && is_string($response->detail)) { |
|
| 151 | + $buffer[] = $response->detail; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + // {"non_field_errors": [ "string...", ...]} |
|
| 155 | + if (isset($response->non_field_errors) && is_array($response->non_field_errors)) { |
|
| 156 | + foreach ($response->non_field_errors as $message) { |
|
| 157 | + if (is_string($message)) { |
|
| 158 | + $buffer[] = $message; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + return empty($buffer) ? null : $buffer; |
|
| 164 | + } |
|
| 165 | 165 | } |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | * @throws ConnectionException |
| 46 | 46 | * @return no-return |
| 47 | 47 | */ |
| 48 | - public static function throwCurlResult(int $code, string|bool $curlResult): never |
|
| 48 | + public static function throwCurlResult(int $code, string | bool $curlResult): never |
|
| 49 | 49 | { |
| 50 | 50 | $exception = new self(self::reasonPhrase($code), $code); |
| 51 | 51 | $exception->responseCode = $code; |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | * @noinspection JsonEncodingApiUsageInspection |
| 110 | 110 | * @noinspection RedundantSuppression |
| 111 | 111 | */ |
| 112 | - $result = json_decode((string)$this->responseBodyRaw, false); |
|
| 112 | + $result = json_decode((string) $this->responseBodyRaw, false); |
|
| 113 | 113 | return is_object($result) ? $result : null; |
| 114 | 114 | } |
| 115 | 115 | |
@@ -59,8 +59,7 @@ |
||
| 59 | 59 | return sprintf('%s %s', $code, self::HTTP_STATUS[$code] ?? "UNKNOWN_PHRASE"); |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) |
|
| 63 | - { |
|
| 62 | + public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) { |
|
| 64 | 63 | // trigger E_USER_NOTICE if code is not known |
| 65 | 64 | $isHttpCode = 100 <= $code && $code < 600; |
| 66 | 65 | $isKnownHttpCode = $isHttpCode && isset(self::HTTP_STATUS[$code]); |