Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\S3;
3
 
4
use Aws\Api\Parser\AbstractParser;
5
use Aws\Api\Parser\Exception\ParserException;
6
use Aws\Api\StructureShape;
7
use Aws\CommandInterface;
8
use Aws\Exception\AwsException;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\StreamInterface;
11
 
12
/**
13
 * Converts errors returned with a status code of 200 to a retryable error type.
14
 *
15
 * @internal
16
 */
17
class AmbiguousSuccessParser extends AbstractParser
18
{
19
    private static $ambiguousSuccesses = [
20
        'UploadPart' => true,
21
        'UploadPartCopy' => true,
22
        'CopyObject' => true,
23
        'CompleteMultipartUpload' => true,
24
    ];
25
 
26
    /** @var callable */
27
    private $errorParser;
28
    /** @var string */
29
    private $exceptionClass;
30
 
31
    public function __construct(
32
        callable $parser,
33
        callable $errorParser,
34
        $exceptionClass = AwsException::class
35
    ) {
36
        $this->parser = $parser;
37
        $this->errorParser = $errorParser;
38
        $this->exceptionClass = $exceptionClass;
39
    }
40
 
41
    public function __invoke(
42
        CommandInterface $command,
43
        ResponseInterface $response
44
    ) {
45
        if (200 === $response->getStatusCode()
46
            && isset(self::$ambiguousSuccesses[$command->getName()])
47
        ) {
48
            $errorParser = $this->errorParser;
49
            try {
50
                $parsed = $errorParser($response);
51
            } catch (ParserException $e) {
52
                $parsed = [
53
                    'code' => 'ConnectionError',
54
                    'message' => "An error connecting to the service occurred"
55
                        . " while performing the " . $command->getName()
56
                        . " operation."
57
                ];
58
            }
59
            if (isset($parsed['code']) && isset($parsed['message'])) {
60
                throw new $this->exceptionClass(
61
                    $parsed['message'],
62
                    $command,
63
                    ['connection_error' => true]
64
                );
65
            }
66
        }
67
 
68
        $fn = $this->parser;
69
        return $fn($command, $response);
70
    }
71
 
72
    public function parseMemberFromStream(
73
        StreamInterface $stream,
74
        StructureShape $member,
75
        $response
76
    ) {
77
        return $this->parser->parseMemberFromStream($stream, $member, $response);
78
    }
79
}