@@ -83,13 +83,19 @@ class Response
8383 */
8484 private $ debug ;
8585
86- public function __construct (ResponseInterface $ response , HttpClientInterface $ httpClient , LoggerInterface $ logger , AwsErrorFactoryInterface $ awsErrorFactory = null , bool $ debug = false )
86+ /**
87+ * @var array<string, string>
88+ */
89+ private $ exceptionMapping ;
90+
91+ public function __construct (ResponseInterface $ response , HttpClientInterface $ httpClient , LoggerInterface $ logger , AwsErrorFactoryInterface $ awsErrorFactory = null , bool $ debug = false , array $ exceptionMapping = [])
8792 {
8893 $ this ->httpResponse = $ response ;
8994 $ this ->httpClient = $ httpClient ;
9095 $ this ->logger = $ logger ;
9196 $ this ->awsErrorFactory = $ awsErrorFactory ?? new ChainAwsErrorFactory ();
9297 $ this ->debug = $ debug ;
98+ $ this ->exceptionMapping = $ exceptionMapping ;
9399 }
94100
95101 public function __destruct ()
@@ -373,24 +379,27 @@ private function defineResolveStatus(): void
373379 }
374380
375381 if (300 <= $ statusCode ) {
376- $ awsErrorFactory = $ this ->awsErrorFactory ;
377- $ httpResponse = $ this ->httpResponse ;
378- $ this ->resolveResult = static function () use ($ awsErrorFactory , $ httpResponse ): HttpException {
379- try {
380- $ awsError = $ awsErrorFactory ->createFromResponse ($ httpResponse );
381- } catch (UnparsableResponse $ e ) {
382- $ awsError = null ;
383- }
384- $ statusCode = $ httpResponse ->getStatusCode ();
385- if (500 <= $ statusCode ) {
386- return new ServerException ($ httpResponse , $ awsError );
387- }
382+ try {
383+ $ awsError = $ this ->awsErrorFactory ->createFromResponse ($ this ->httpResponse );
384+ } catch (UnparsableResponse $ e ) {
385+ $ awsError = null ;
386+ }
388387
389- if (400 <= $ statusCode ) {
390- return new ClientException ($ httpResponse , $ awsError );
391- }
388+ if ((null !== $ awsCode = ($ awsError ? $ awsError ->getCode () : null )) && isset ($ this ->exceptionMapping [$ awsCode ])) {
389+ $ exceptionClass = $ this ->exceptionMapping [$ awsCode ];
390+ } elseif (500 <= $ statusCode ) {
391+ $ exceptionClass = ServerException::class;
392+ } elseif (400 <= $ statusCode ) {
393+ $ exceptionClass = ClientException::class;
394+ } else {
395+ $ exceptionClass = RedirectionException::class;
396+ }
392397
393- return new RedirectionException ($ httpResponse , $ awsError );
398+ $ httpResponse = $ this ->httpResponse ;
399+ /** @psalm-suppress MoreSpecificReturnType */
400+ $ this ->resolveResult = static function () use ($ exceptionClass , $ httpResponse , $ awsError ): HttpException {
401+ /** @psalm-suppress LessSpecificReturnStatement */
402+ return new $ exceptionClass ($ httpResponse , $ awsError );
394403 };
395404
396405 return ;
0 commit comments