1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Lyal\Checkr; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Lyal\Checkr\Exceptions\Client\BadRequest; |
8
|
|
|
use Lyal\Checkr\Exceptions\Client\Conflict; |
9
|
|
|
use Lyal\Checkr\Exceptions\Client\Forbidden; |
10
|
|
|
use Lyal\Checkr\Exceptions\Client\NotFound; |
11
|
|
|
use Lyal\Checkr\Exceptions\Client\Unauthorized; |
12
|
|
|
use Lyal\Checkr\Exceptions\Server\InternalServerError; |
13
|
|
|
use Lyal\Checkr\Exceptions\UnhandledRequestError; |
14
|
|
|
|
15
|
|
|
class RequestErrorHandler |
16
|
|
|
{ |
17
|
|
|
private $exception; |
18
|
|
|
private $body; |
19
|
|
|
|
20
|
|
|
public function __construct($exception) |
21
|
|
|
{ |
22
|
|
|
$this->exception = $exception; |
23
|
|
|
$this->body = $exception->getResponse()->getBody(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function handleError() |
27
|
|
|
{ |
28
|
|
|
$errorCode = $this->exception->getResponse()->getStatusCode(); |
29
|
|
|
|
30
|
|
|
if (method_exists($this, 'handle' . $errorCode)) { |
31
|
|
|
$this->{'handle' . $errorCode}(); |
32
|
|
|
} |
33
|
|
|
$this->handleUnknown(); |
34
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function handle400() |
38
|
|
|
{ |
39
|
|
|
throw new BadRequest($this->body); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function handle401() |
43
|
|
|
{ |
44
|
|
|
throw new Unauthorized($this->body); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function handle403() |
48
|
|
|
{ |
49
|
|
|
throw new Forbidden($this->body); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function handle404() |
53
|
|
|
{ |
54
|
|
|
throw new NotFound($this->body); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function handle409() |
58
|
|
|
{ |
59
|
|
|
throw new Conflict($this->body); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function handle500() |
63
|
|
|
{ |
64
|
|
|
throw new InternalServerError($this->body); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
private function handleUnknown() |
69
|
|
|
{ |
70
|
|
|
throw new UnhandledRequestError($this->exception->getResponse()->getStatusCode(), $this->body); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
} |