Skip to content

Commit b6787f1

Browse files
add error handling
1 parent 28c8d6c commit b6787f1

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

lib/API/Client.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,18 @@ public function get(HTTPRequest $request)
247247
];
248248
$response = $this->getHttpClient()
249249
->get($request->buildEndPoint(), $data);
250-
return new HTTPResponse($response);
250+
251+
$httpResponse = new HTTPResponse($response);
252+
return $httpResponse;
251253
} catch (RequestException $e) {
252-
echo $e->getMessage();
254+
if ($e->hasResponse()) {
255+
$httpResponse = new HTTPResponse($e);
256+
$httpResponse->setStatusCode($e->getCode());
257+
return $httpResponse;
258+
} else{
259+
throw $e;
260+
}
261+
253262
}
254263

255264
}

lib/API/HTTPResponse.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,46 @@ class HTTPResponse {
2323

2424
private $response = null ;
2525

26+
private $statusCode = 200;
27+
28+
private $result = null;
29+
2630
public function __construct($response) {
2731
$this->response = $response;
2832
}
2933

30-
public function getStatusCode(){
34+
/**
35+
* @return null
36+
*/
37+
public function getStatusCode()
38+
{
39+
$result = $this->getResult();
40+
if(isset($result['error'])) {
41+
return $result['error']['status'];
42+
} else{
43+
return $this->statusCode;
44+
}
3145

3246
}
3347

48+
/**
49+
* @param null $statusCode
50+
* @return $this;
51+
*/
52+
public function setStatusCode($statusCode)
53+
{
54+
$this->statusCode = $statusCode;
55+
return $this;
56+
}
57+
3458
/**
3559
* @return array
3660
*/
3761
public function getResult() {
38-
return json_decode($this->getResponse()->getBody()->getContents(), true);
62+
if(empty($this->result)){
63+
$this->result = json_decode($this->getResponse()->getBody()->getContents(), true);
64+
}
65+
return $this->result ;
3966
}
4067

4168
/**
@@ -57,4 +84,18 @@ public function getToken() {
5784
}
5885
}
5986

87+
public function hasError() {
88+
89+
$result = $this->getResult();
90+
return isset($result['error']);
91+
}
92+
93+
public function getError() {
94+
$result = $this->getResult();
95+
if(isset($result['error'])) {
96+
return $result['error']['text'];
97+
}
98+
99+
}
100+
60101
}

test/ClientTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@ public function testGetRequest() {
4242

4343
$this->assertArrayHasKey('data',$result->getResult());
4444
}
45+
46+
public function testErrorResult() {
47+
$request = new HTTPRequest('leave/search?fromDate="2005-11-30"&toDate="2005-12-30"');
48+
$result = $this->client->get($request);
49+
$this->assertTrue($result->hasError());
50+
$this->assertEquals('toDate must be a valid date. Sample format: "2005-12-30"',$result->getError());
51+
$this->assertEquals(202,$result->getStatusCode());
52+
}
4553
}

0 commit comments

Comments
 (0)