1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (C) 2013-2016 Mailgun |
5
|
|
|
* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Mailgun\Exception; |
11
|
|
|
|
12
|
|
|
use Mailgun\Exception; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Tobias Nyholm <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
final class HttpClientException extends \RuntimeException implements Exception |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ResponseInterface|null |
22
|
|
|
*/ |
23
|
|
|
private $response; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $responseBody; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $message |
32
|
|
|
* @param int $code |
33
|
|
|
* @param ResponseInterface|null $response |
34
|
|
|
*/ |
35
|
|
|
public function __construct($message, $code, ResponseInterface $response = null) |
36
|
|
|
{ |
37
|
|
|
parent::__construct($message, $code); |
38
|
|
|
|
39
|
|
|
if ($response) { |
40
|
|
|
$this->response = $response; |
41
|
|
|
$body = $response->getBody()->__toString(); |
42
|
|
|
if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== 0) { |
43
|
|
|
$this->responseBody['message'] = $body; |
44
|
|
|
} else { |
45
|
|
|
$this->responseBody = json_decode($body, true); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function badRequest(ResponseInterface $response = null) |
51
|
|
|
{ |
52
|
|
|
return new self('The parameters passed to the API were invalid. Check your inputs!', 400, $response); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function unauthorized(ResponseInterface $response = null) |
56
|
|
|
{ |
57
|
|
|
return new self('Your credentials are incorrect.', 401, $response); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function requestFailed(ResponseInterface $response = null) |
61
|
|
|
{ |
62
|
|
|
return new self('Parameters were valid but request failed. Try again.', 402, $response); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function notFound(ResponseInterface $response = null) |
66
|
|
|
{ |
67
|
|
|
return new self('The endpoint you tried to access does not exist. Check your URL.', 404, $response); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return ResponseInterface |
72
|
|
|
*/ |
73
|
|
|
public function getResponse() |
74
|
|
|
{ |
75
|
|
|
return $this->response; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function getResponseBody() |
82
|
|
|
{ |
83
|
|
|
return $this->responseBody; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..