Repository navigation
Expand file tree
/
Copy pathErrorMiddleware.php
More file actions
67 lines (60 loc) · 2.17 KB
/
Copy pathErrorMiddleware.php
File metadata and controls
67 lines (60 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php declare(strict_types=1);
namespace ApiClients\Client\Github\Middleware;
use ApiClients\Client\Github\ApiErrorException;
use ApiClients\Foundation\Middleware\Annotation\SecondLast;
use ApiClients\Foundation\Middleware\MiddlewareInterface;
use ApiClients\Foundation\Middleware\PostTrait;
use ApiClients\Foundation\Middleware\PreTrait;
use ApiClients\Foundation\Transport\ParsedContentsInterface;
use ApiClients\Tools\Psr7\HttpStatusExceptions\BadRequestException;
use ApiClients\Tools\Psr7\HttpStatusExceptions\UnprocessableEntityException;
use React\Promise\CancellablePromiseInterface;
use function React\Promise\reject;
use Throwable;
final class ErrorMiddleware implements MiddlewareInterface
{
use PreTrait;
use PostTrait;
/**
* @param Throwable $throwable
* @param string $transactionId
* @param array $options
* @return CancellablePromiseInterface
* @SecondLast()
*/
public function error(
Throwable $throwable,
string $transactionId,
array $options = []
): CancellablePromiseInterface {
if ($throwable instanceof UnprocessableEntityException ||
$throwable instanceof BadRequestException
) {
$response = $throwable->getResponse();
$body = $response->getBody();
if (!($body instanceof ParsedContentsInterface)) {
return reject($throwable);
}
$contents = $body->getParsedContents();
$message = $contents['message'] ?? $throwable->getMessage();
if (isset($contents['errors'])) {
return reject(
ApiErrorException::createWithErrors(
$message,
$throwable->getCode(),
$contents['errors'],
$throwable
)
);
}
return reject(
ApiErrorException::create(
$message,
$throwable->getCode(),
$throwable
)
);
}
return reject($throwable);
}
}