1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace CloudPlayDev\ConfluenceClient\Exception; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Throwable; |
9
|
|
|
use Webmozart\Assert\Assert; |
10
|
|
|
|
11
|
|
|
class HttpClientException extends ConfluencePhpClientException |
12
|
|
|
{ |
13
|
|
|
private ResponseInterface $response; |
14
|
|
|
|
15
|
|
|
public function __construct(string $message, int $code, ResponseInterface $response) |
16
|
|
|
{ |
17
|
|
|
parent::__construct($message, $code); |
18
|
|
|
$this->response = $response; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public static function badRequest(ResponseInterface $response): HttpClientException |
22
|
|
|
{ |
23
|
|
|
$validationMessage = self::extractValidationMessage($response); |
24
|
|
|
|
25
|
|
|
$message = sprintf("The parameters passed to the API were invalid. Check your inputs!\n\n%s", $validationMessage); |
26
|
|
|
|
27
|
|
|
return new self($message, 400, $response); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function unauthorized(ResponseInterface $response): HttpClientException |
31
|
|
|
{ |
32
|
|
|
return new self('Your credentials are incorrect.', 401, $response); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function requestFailed(ResponseInterface $response): HttpClientException |
36
|
|
|
{ |
37
|
|
|
return new self('Parameters were valid but request failed. Try again.', 402, $response); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function notFound(ResponseInterface $response): HttpClientException |
41
|
|
|
{ |
42
|
|
|
return new self('The endpoint you have tried to access does not exist.', 404, $response); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function conflict(ResponseInterface $response): HttpClientException |
46
|
|
|
{ |
47
|
|
|
return new self('Request conflicts with current state of the target resource.', 409, $response); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function payloadTooLarge(ResponseInterface $response): HttpClientException |
51
|
|
|
{ |
52
|
|
|
return new self('Payload too large, your total attachment size is too big.', 413, $response); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function tooManyRequests(ResponseInterface $response): HttpClientException |
56
|
|
|
{ |
57
|
|
|
return new self('Too many requests.', 429, $response); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function forbidden(ResponseInterface $response): HttpClientException |
61
|
|
|
{ |
62
|
|
|
$validationMessage = self::extractValidationMessage($response); |
63
|
|
|
|
64
|
|
|
$message = sprintf("Forbidden!\n\n%s", $validationMessage); |
65
|
|
|
|
66
|
|
|
return new self($message, 403, $response); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param ResponseInterface $response |
71
|
|
|
* @param string $jsonField |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
private static function extractValidationMessage(ResponseInterface $response, string $jsonField = 'message'): string |
75
|
|
|
{ |
76
|
|
|
|
77
|
|
|
$validationMessage = $response->getBody()->getContents(); |
78
|
|
|
|
79
|
|
|
try { |
80
|
|
|
if (str_starts_with($response->getHeaderLine('Content-Type'), 'application/json')) { |
81
|
|
|
$jsonDecoded = json_decode($validationMessage, true, 512, JSON_THROW_ON_ERROR); |
82
|
|
|
Assert::isArray($jsonDecoded); |
83
|
|
|
Assert::keyExists($jsonDecoded, $jsonField); |
84
|
|
|
Assert::string($jsonDecoded[$jsonField]); |
85
|
|
|
$validationMessage = $jsonDecoded[$jsonField]; |
86
|
|
|
} |
87
|
|
|
} catch (Throwable) { |
88
|
|
|
return $validationMessage; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $validationMessage; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return ResponseInterface |
96
|
|
|
*/ |
97
|
|
|
public function getResponse(): ResponseInterface |
98
|
|
|
{ |
99
|
|
|
return $this->response; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|