Skip to content

Commit d810856

Browse files
authored
Merge pull request #337 from Art4/335-catch-error-on-empty-response-body
Restore BC in `all()` methods with empty Redmine response
2 parents a1fd6dd + 5dbecd0 commit d810856

40 files changed

+669
-267
lines changed

src/Redmine/Api/AbstractApi.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Redmine\Api;
66
use Redmine\Client\Client;
7+
use Redmine\Exception;
78
use Redmine\Exception\SerializerException;
89
use Redmine\Serializer\JsonSerializer;
910
use Redmine\Serializer\PathSerializer;
@@ -169,16 +170,20 @@ protected function sanitizeParams(array $defaults, array $params)
169170
* @param string $endpoint API end point
170171
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
171172
*
172-
* @return array|false elements found
173+
* @return array|string|false elements found or error message or false
173174
*/
174175
protected function retrieveAll($endpoint, array $params = [])
175176
{
176177
@trigger_error('`'.__METHOD__.'()` is deprecated since v2.2.0, use `retrieveData()` instead.', E_USER_DEPRECATED);
177178

178179
try {
179180
$data = $this->retrieveData(strval($endpoint), $params);
180-
} catch (SerializerException $e) {
181-
$data = false;
181+
} catch (Exception $e) {
182+
if ($this->client->getLastResponseBody() === '') {
183+
return false;
184+
}
185+
186+
return $e->getMessage();
182187
}
183188

184189
return $data;
@@ -307,6 +312,7 @@ protected function attachCustomFieldXML(SimpleXMLElement $xml, array $fields)
307312
private function getLastResponseBodyAsArray(): array
308313
{
309314
$body = $this->client->getLastResponseBody();
315+
310316
$contentType = $this->client->getLastResponseContentType();
311317
$returnData = null;
312318

src/Redmine/Api/CustomField.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Redmine\Api;
44

5+
use Redmine\Exception;
6+
use Redmine\Exception\SerializerException;
7+
58
/**
69
* Listing custom fields.
710
*
@@ -20,6 +23,8 @@ class CustomField extends AbstractApi
2023
*
2124
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
2225
*
26+
* @throws SerializerException if response body could not be converted into array
27+
*
2328
* @return array list of custom fields found
2429
*/
2530
final public function list(array $params = []): array
@@ -38,13 +43,21 @@ final public function list(array $params = []): array
3843
*
3944
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
4045
*
41-
* @return array list of custom fields found
46+
* @return array|string|false list of custom fields found or error message or false
4247
*/
4348
public function all(array $params = [])
4449
{
4550
@trigger_error('`'.__METHOD__.'()` is deprecated since v2.4.0, use `'.__CLASS__.'::list()` instead.', E_USER_DEPRECATED);
4651

47-
return $this->list($params);
52+
try {
53+
return $this->list($params);
54+
} catch (Exception $e) {
55+
if ($this->client->getLastResponseBody() === '') {
56+
return false;
57+
}
58+
59+
return $e->getMessage();
60+
}
4861
}
4962

5063
/**

src/Redmine/Api/Group.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace Redmine\Api;
44

5-
use Exception;
5+
use Redmine\Exception;
66
use Redmine\Exception\MissingParameterException;
7+
use Redmine\Exception\SerializerException;
78
use Redmine\Serializer\PathSerializer;
89
use Redmine\Serializer\XmlSerializer;
910

@@ -25,6 +26,8 @@ class Group extends AbstractApi
2526
*
2627
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
2728
*
29+
* @throws SerializerException if response body could not be converted into array
30+
*
2831
* @return array list of groups found
2932
*/
3033
final public function list(array $params = []): array
@@ -43,13 +46,21 @@ final public function list(array $params = []): array
4346
*
4447
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
4548
*
46-
* @return array list of groups found
49+
* @return array|string|false list of groups found or error message or false
4750
*/
4851
public function all(array $params = [])
4952
{
5053
@trigger_error('`'.__METHOD__.'()` is deprecated since v2.4.0, use `'.__CLASS__.'::list()` instead.', E_USER_DEPRECATED);
5154

52-
return $this->list($params);
55+
try {
56+
return $this->list($params);
57+
} catch (Exception $e) {
58+
if ($this->client->getLastResponseBody() === '') {
59+
return false;
60+
}
61+
62+
return $e->getMessage();
63+
}
5364
}
5465

5566
/**
@@ -114,7 +125,7 @@ public function create(array $params = [])
114125
*/
115126
public function update($id, array $params = [])
116127
{
117-
throw new Exception('Not implemented');
128+
throw new \Exception('Not implemented');
118129
}
119130

120131
/**

src/Redmine/Api/Issue.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Redmine\Api;
44

5+
use Redmine\Exception;
6+
use Redmine\Exception\SerializerException;
57
use Redmine\Serializer\JsonSerializer;
68
use Redmine\Serializer\PathSerializer;
79
use Redmine\Serializer\XmlSerializer;
@@ -38,6 +40,8 @@ class Issue extends AbstractApi
3840
*
3941
* @param array $params the additional parameters (cf available $params above)
4042
*
43+
* @throws SerializerException if response body could not be converted into array
44+
*
4145
* @return array list of issues found
4246
*/
4347
final public function list(array $params = []): array
@@ -64,13 +68,21 @@ final public function list(array $params = []): array
6468
*
6569
* @param array $params the additional parameters (cf available $params above)
6670
*
67-
* @return array list of issues found
71+
* @return array|string|false list of issues found or error message or false
6872
*/
6973
public function all(array $params = [])
7074
{
7175
@trigger_error('`'.__METHOD__.'()` is deprecated since v2.4.0, use `'.__CLASS__.'::list()` instead.', E_USER_DEPRECATED);
7276

73-
return $this->list($params);
77+
try {
78+
return $this->list($params);
79+
} catch (Exception $e) {
80+
if ($this->client->getLastResponseBody() === '') {
81+
return false;
82+
}
83+
84+
return $e->getMessage();
85+
}
7486
}
7587

7688
/**

src/Redmine/Api/IssueCategory.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Redmine\Api;
44

5+
use Redmine\Exception;
56
use Redmine\Exception\InvalidParameterException;
67
use Redmine\Exception\MissingParameterException;
8+
use Redmine\Exception\SerializerException;
79
use Redmine\Serializer\PathSerializer;
810
use Redmine\Serializer\XmlSerializer;
911

@@ -27,6 +29,7 @@ class IssueCategory extends AbstractApi
2729
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
2830
*
2931
* @throws InvalidParameterException if $projectIdentifier is not of type int or string
32+
* @throws SerializerException if response body could not be converted into array
3033
*
3134
* @return array list of issue categories found
3235
*/
@@ -54,13 +57,21 @@ final public function listByProject($projectIdentifier, array $params = []): arr
5457
* @param string|int $project project id or literal identifier
5558
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
5659
*
57-
* @return array list of issue categories found
60+
* @return array|string|false list of issue categories found or error message or false
5861
*/
5962
public function all($project, array $params = [])
6063
{
6164
@trigger_error('`'.__METHOD__.'()` is deprecated since v2.4.0, use `'.__CLASS__.'::listByProject()` instead.', E_USER_DEPRECATED);
6265

63-
return $this->listByProject(strval($project), $params);
66+
try {
67+
return $this->listByProject(strval($project), $params);
68+
} catch (Exception $e) {
69+
if ($this->client->getLastResponseBody() === '') {
70+
return false;
71+
}
72+
73+
return $e->getMessage();
74+
}
6475
}
6576

6677
/**

src/Redmine/Api/IssuePriority.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Redmine\Api;
44

5+
use Redmine\Exception;
6+
use Redmine\Exception\SerializerException;
7+
58
/**
69
* Listing issue priorities.
710
*
@@ -20,6 +23,8 @@ class IssuePriority extends AbstractApi
2023
*
2124
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
2225
*
26+
* @throws SerializerException if response body could not be converted into array
27+
*
2328
* @return array list of issue priorities found
2429
*/
2530
final public function list(array $params = []): array
@@ -38,12 +43,20 @@ final public function list(array $params = []): array
3843
*
3944
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
4045
*
41-
* @return array list of issue priorities found
46+
* @return array|string|false list of issue priorities found or error message or false
4247
*/
4348
public function all(array $params = [])
4449
{
4550
@trigger_error('`'.__METHOD__.'()` is deprecated since v2.4.0, use `'.__CLASS__.'::list()` instead.', E_USER_DEPRECATED);
4651

47-
return $this->list($params);
52+
try {
53+
return $this->list($params);
54+
} catch (Exception $e) {
55+
if ($this->client->getLastResponseBody() === '') {
56+
return false;
57+
}
58+
59+
return $e->getMessage();
60+
}
4861
}
4962
}

src/Redmine/Api/IssueRelation.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Redmine\Api;
44

5+
use Redmine\Exception;
6+
use Redmine\Exception\SerializerException;
57
use Redmine\Serializer\JsonSerializer;
68

79
/**
@@ -23,6 +25,8 @@ class IssueRelation extends AbstractApi
2325
* @param int $issueId the issue id
2426
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
2527
*
28+
* @throws SerializerException if response body could not be converted into array
29+
*
2630
* @return array list of relations found
2731
*/
2832
final public function listByIssueId(int $issueId, array $params = []): array
@@ -42,13 +46,21 @@ final public function listByIssueId(int $issueId, array $params = []): array
4246
* @param int $issueId the issue id
4347
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
4448
*
45-
* @return array list of relations found
49+
* @return array|string|false list of relations found or error message or false
4650
*/
4751
public function all($issueId, array $params = [])
4852
{
4953
@trigger_error('`'.__METHOD__.'()` is deprecated since v2.4.0, use `'.__CLASS__.'::listByIssueId()` instead.', E_USER_DEPRECATED);
5054

51-
return $this->listByIssueId($issueId, $params);
55+
try {
56+
return $this->listByIssueId($issueId, $params);
57+
} catch (Exception $e) {
58+
if ($this->client->getLastResponseBody() === '') {
59+
return false;
60+
}
61+
62+
return $e->getMessage();
63+
}
5264
}
5365

5466
/**

src/Redmine/Api/IssueStatus.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Redmine\Api;
44

5+
use Redmine\Exception;
6+
use Redmine\Exception\SerializerException;
7+
58
/**
69
* Listing issue statuses.
710
*
@@ -20,6 +23,8 @@ class IssueStatus extends AbstractApi
2023
*
2124
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
2225
*
26+
* @throws SerializerException if response body could not be converted into array
27+
*
2328
* @return array list of issue statuses found
2429
*/
2530
final public function list(array $params = []): array
@@ -38,13 +43,21 @@ final public function list(array $params = []): array
3843
*
3944
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
4045
*
41-
* @return array list of issue statuses found
46+
* @return array|string|false list of issue statuses found or error message or false
4247
*/
4348
public function all(array $params = [])
4449
{
4550
@trigger_error('`'.__METHOD__.'()` is deprecated since v2.4.0, use `'.__CLASS__.'::list()` instead.', E_USER_DEPRECATED);
4651

47-
return $this->list($params);
52+
try {
53+
return $this->list($params);
54+
} catch (Exception $e) {
55+
if ($this->client->getLastResponseBody() === '') {
56+
return false;
57+
}
58+
59+
return $e->getMessage();
60+
}
4861
}
4962

5063
/**

src/Redmine/Api/Membership.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Redmine\Api;
44

5+
use Redmine\Exception;
56
use Redmine\Exception\InvalidParameterException;
67
use Redmine\Exception\MissingParameterException;
8+
use Redmine\Exception\SerializerException;
79
use Redmine\Serializer\XmlSerializer;
810

911
/**
@@ -26,6 +28,7 @@ class Membership extends AbstractApi
2628
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
2729
*
2830
* @throws InvalidParameterException if $projectIdentifier is not of type int or string
31+
* @throws SerializerException if response body could not be converted into array
2932
*
3033
* @return array list of memberships found
3134
*/
@@ -53,13 +56,21 @@ final public function listByProject($projectIdentifier, array $params = []): arr
5356
* @param string|int $project project id or literal identifier
5457
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
5558
*
56-
* @return array list of memberships found
59+
* @return array|string|false list of memberships found or error message or false
5760
*/
5861
public function all($project, array $params = [])
5962
{
6063
@trigger_error('`'.__METHOD__.'()` is deprecated since v2.4.0, use `'.__CLASS__.'::listByProject()` instead.', E_USER_DEPRECATED);
6164

62-
return $this->listByProject(strval($project), $params);
65+
try {
66+
return $this->listByProject(strval($project), $params);
67+
} catch (Exception $e) {
68+
if ($this->client->getLastResponseBody() === '') {
69+
return false;
70+
}
71+
72+
return $e->getMessage();
73+
}
6374
}
6475

6576
/**

0 commit comments

Comments
 (0)