3 namespace app\Github\Exception
;
5 use app\Github\Exception
;
7 use http\Client\Response
;
9 class RequestException
extends \Exception
implements Exception
11 protected $errors = [];
13 function __construct(Response
$response) {
14 if (($h = $response->getHeader("Content-Type", Header
::class))
15 && $h->match("application/json", Header
::MATCH_WORD
)
16 && $failure = json_decode($response->getBody())) {
17 $message = $failure->message
;
18 if (isset($failure->errors
)) {
19 $this->errors
= (array) $failure->errors
;
22 $message = $response->getBody()->toString();
25 if (!strlen($message)) {
26 $message = $response->getTransferInfo("error");
29 parent
::__construct($message, $response->getResponseCode(), null);
32 function getErrors() {
36 function getErrorsAsString() {
38 "missing" => "The resource %1\$s does not exist\n",
39 "missing_field" => "Missing field %2\$s of resource %1\$s\n",
40 "invalid" => "Invalid formatting of field %2\$s of resource %1\$s\n",
41 "already_exists" => "A resource %1\$s with the same value of field %2\$s already exists\n",
48 $errors = "JSON errors:\n";
49 foreach ($this->errors
as $error) {
50 $errors .= sprintf($reasons[$error->code
], $error->resource, $error->field
);
54 function __toString() {
55 return parent
::__toString() . "\n". $this->getErrorsAsString();