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