66use Psr \Http \Message \RequestInterface ;
77use Psr \Http \Message \ResponseInterface ;
88
9- /**
10- *
11- *
12- */
139class APIStatusException extends APIException
1410{
15- /** @var string */
16- protected const DESC = 'Increase API Status Error ' ;
11+ /** @var string */
12+ protected const DESC = 'Increase API Status Error ' ;
1713
18- /** @var int|null $status */
19- public ?int $ status ;
14+ public ?int $ status ;
2015
21- /**
22- * @param RequestInterface $request
23- * @param ResponseInterface $response
24- * @param string $message
25- *
26- * @return self
27- */
28- public static function from (
29- RequestInterface $ request , ResponseInterface $ response , string $ message = ''
30- ): self {
31- $ status = $ response ->getStatusCode ();
32- $ STAINLESS_FIXME_key = Util::dig ($ body , 'type ' )
16+ public function __construct (
17+ public RequestInterface $ request ,
18+ ResponseInterface $ response ,
19+ ?\Throwable $ previous = null ,
20+ string $ message = '' ,
21+ ) {
22+ $ this ->response = $ response ;
23+ $ this ->status = $ response ->getStatusCode ();
3324
34- $ cls = match (true )
35- {
25+ $ summary = Util::prettyEncodeJson (['status ' => $ this ->status , 'body ' => Util::decodeJson ($ response ->getBody ())]);
3626
37- $ status === null && $ key === InvalidParametersError::type => InvalidParametersError::class,
38- $ status === null && $ key === MalformedRequestError::type => MalformedRequestError::class,
39- $ status === null && $ key === InvalidAPIKeyError::type => InvalidAPIKeyError::class,
40- $ status === null && $ key === EnvironmentMismatchError::type => EnvironmentMismatchError::class,
41- $ status === null && $ key === InsufficientPermissionsError::type => InsufficientPermissionsError::class,
42- $ status === null && $ key === PrivateFeatureError::type => PrivateFeatureError::class,
43- $ status === null && $ key === APIMethodNotFoundError::type => APIMethodNotFoundError::class,
44- $ status === null && $ key === ObjectNotFoundError::type => ObjectNotFoundError::class,
45- $ status === null && $ key === IdempotencyKeyAlreadyUsedError::type => IdempotencyKeyAlreadyUsedError::class,
46- $ status === null && $ key === InvalidOperationError::type => InvalidOperationError::class,
47- $ status === null && $ key === RateLimitedError::type => RateLimitedError::class,
48- $ status >= 500 && $ key === InternalServerException::type => InternalServerException::class,
49- $ status === 400 => BadRequestException::class,
50- $ status === 401 => AuthenticationException::class,
51- $ status === 403 => PermissionDeniedException::class,
52- $ status === 404 => NotFoundException::class,
53- $ status === 409 => ConflictException::class,
54- $ status === 422 => UnprocessableEntityException::class,
55- $ status === 429 => RateLimitException::class,
56- default => APIStatusException::class
27+ if ('' != $ message ) {
28+ $ summary .= $ message .PHP_EOL .$ summary ;
29+ }
5730
58- };
59-
60- return new $ cls (request: $ request , response: $ response , message: $ message );
61- }
31+ parent ::__construct (request: $ request , message: $ summary , previous: $ previous );
32+ }
6233
63- /**
64- * @param RequestInterface $request
65- * @param \Throwable|null $previous
66- * @param ResponseInterface $response
67- * @param string $message
68- */
69- function __construct (
70- public RequestInterface $ request ,
71- ResponseInterface $ response ,
72- ?\Throwable $ previous = null ,
73- string $ message = '' ,
74- ) {
75- $ this ->response = $ response ;
76- $ this ->status = $ response ->getStatusCode ();
34+ public static function from (
35+ RequestInterface $ request ,
36+ ResponseInterface $ response ,
37+ string $ message = ''
38+ ): self {
39+ $ status = $ response ->getStatusCode ();
40+ $ body = Util::decodeJson ($ response ->getBody ());
41+ $ key = Util::dig ($ body , 'type ' );
7742
78- $ summary = Util::prettyEncodeJson (['status ' => $ this ->status , 'body ' => Util::decodeJson ($ response ->getBody ())]);
43+ $ cls = match (true ) {
44+ 400 === $ status && InvalidParametersError::type === $ key => InvalidParametersError::class,
45+ 400 === $ status && MalformedRequestError::type === $ key => MalformedRequestError::class,
46+ 401 === $ status && InvalidAPIKeyError::type === $ key => InvalidAPIKeyError::class,
47+ 403 === $ status && EnvironmentMismatchError::type === $ key => EnvironmentMismatchError::class,
48+ 403 === $ status && InsufficientPermissionsError::type === $ key => InsufficientPermissionsError::class,
49+ 403 === $ status && PrivateFeatureError::type === $ key => PrivateFeatureError::class,
50+ 404 === $ status && APIMethodNotFoundError::type === $ key => APIMethodNotFoundError::class,
51+ 404 === $ status && ObjectNotFoundError::type === $ key => ObjectNotFoundError::class,
52+ 409 === $ status && IdempotencyKeyAlreadyUsedError::type === $ key => IdempotencyKeyAlreadyUsedError::class,
53+ 409 === $ status && InvalidOperationError::type === $ key => InvalidOperationError::class,
54+ 429 === $ status && RateLimitedError::type === $ key => RateLimitedError::class,
55+ $ status >= 500 && InternalServerException::type === $ key => InternalServerException::class,
56+ 400 === $ status => BadRequestException::class,
57+ 401 === $ status => AuthenticationException::class,
58+ 403 === $ status => PermissionDeniedException::class,
59+ 404 === $ status => NotFoundException::class,
60+ 409 === $ status => ConflictException::class,
61+ 422 === $ status => UnprocessableEntityException::class,
62+ 429 === $ status => RateLimitException::class,
63+ default => APIStatusException::class
64+ };
7965
80- if ('' != $ message ) {
81- $ summary .= $ message . PHP_EOL . $ summary ;
66+ return new $ cls (request: $ request , response: $ response , message: $ message );
8267 }
83-
84- parent ::__construct (request: $ request , message: $ summary , previous: $ previous );
85- }
86- }
68+ }
0 commit comments