forked from ashleyhood/php-lxd
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLxdExceptionThower.php
More file actions
59 lines (49 loc) · 1.84 KB
/
Copy pathLxdExceptionThower.php
File metadata and controls
59 lines (49 loc) · 1.84 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
<?php
namespace Opensaucesystems\Lxd\HttpClient\Plugin;
use Http\Client\Common\Plugin;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Http\Client\Exception\HttpException;
use Opensaucesystems\Lxd\Exception\BadRequestException;
use Opensaucesystems\Lxd\Exception\OperationException;
use Opensaucesystems\Lxd\Exception\AuthenticationFailedException;
use Opensaucesystems\Lxd\Exception\NotFoundException;
use Opensaucesystems\Lxd\Exception\ConflictException;
/**
* Handle LXD errors
*
*/
class LxdExceptionThower implements Plugin
{
/**
* {@inheritdoc}
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
$promise = $next($request);
return $promise->then(function (ResponseInterface $response) use ($request) {
return $response;
}, function (\Exception $e) use ($request) {
if (get_class($e) === HttpException::class) {
$response = $e->getResponse();
if (400 === $response->getStatusCode()) {
throw new BadRequestException($request, $response, $e);
}
if (401 === $response->getStatusCode()) {
throw new OperationException($request, $response, $e);
}
if (403 === $response->getStatusCode()) {
throw new AuthenticationFailedException($request, $response, $e);
}
if (404 === $response->getStatusCode()) {
throw new NotFoundException($request, $response, $e);
}
if (409 === $response->getStatusCode()) {
throw new ConflictException($request, $response, $e);
}
}
throw $e;
});
}
}