Skip to content

Commit 1e7ff16

Browse files
authored
Merge pull request #369 from Art4/add-unexpectedresponseexception-getresponse
Add new method `UnexpectedResponseException::getResponse()`
2 parents 2103392 + 4433cf0 commit 1e7ff16

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+125
-55
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- New method `Redmine\Api\Project::reopen()` to reopen a project.
1515
- New method `Redmine\Api\Project::archive()` to archive a project.
1616
- New method `Redmine\Api\Project::unarchive()` to unarchive a project.
17+
- New method `UnexpectedResponseException::getResponse()` to get the last response responsible for the exception.
1718

1819
### Changed
1920

@@ -35,7 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3536
- New method `Redmine\Api\News::list()` to list news from all project.
3637
- New method `Redmine\Api\News::listByProject()` to list news from a project.
3738
- New method `Redmine\Api\Project::list()` to list projects.
38-
- New method `Redmine\Api\Query::list()` to list projects.
39+
- New method `Redmine\Api\Query::list()` to list queries.
3940
- New method `Redmine\Api\Role::list()` to list roles.
4041
- New method `Redmine\Api\Search::listByQuery()` to list search results by query.
4142
- New method `Redmine\Api\TimeEntry::list()` to list time entries.

src/Redmine/Api/CustomField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final public function list(array $params = []): array
3333
try {
3434
return $this->retrieveData('/custom_fields.json', $params);
3535
} catch (SerializerException $th) {
36-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
36+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
3737
}
3838
}
3939

src/Redmine/Api/Group.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final public function list(array $params = []): array
3737
try {
3838
return $this->retrieveData('/groups.json', $params);
3939
} catch (SerializerException $th) {
40-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
40+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
4141
}
4242
}
4343

src/Redmine/Api/Issue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ final public function list(array $params = []): array
7878
try {
7979
return $this->retrieveData('/issues.json', $params);
8080
} catch (SerializerException $th) {
81-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
81+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
8282
}
8383
}
8484

src/Redmine/Api/IssueCategory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final public function listByProject($projectIdentifier, array $params = []): arr
4646
try {
4747
return $this->retrieveData('/projects/' . strval($projectIdentifier) . '/issue_categories.json', $params);
4848
} catch (SerializerException $th) {
49-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
49+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
5050
}
5151
}
5252

src/Redmine/Api/IssuePriority.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final public function list(array $params = []): array
3333
try {
3434
return $this->retrieveData('/enumerations/issue_priorities.json', $params);
3535
} catch (SerializerException $th) {
36-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
36+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
3737
}
3838
}
3939

src/Redmine/Api/IssueRelation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final public function listByIssueId(int $issueId, array $params = []): array
3535
try {
3636
return $this->retrieveData('/issues/' . strval($issueId) . '/relations.json', $params);
3737
} catch (SerializerException $th) {
38-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
38+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
3939
}
4040
}
4141

src/Redmine/Api/IssueStatus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final public function list(array $params = []): array
3333
try {
3434
return $this->retrieveData('/issue_statuses.json', $params);
3535
} catch (SerializerException $th) {
36-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
36+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
3737
}
3838
}
3939

src/Redmine/Api/Membership.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final public function listByProject($projectIdentifier, array $params = []): arr
4545
try {
4646
return $this->retrieveData('/projects/' . strval($projectIdentifier) . '/memberships.json', $params);
4747
} catch (SerializerException $th) {
48-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
48+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
4949
}
5050
}
5151

src/Redmine/Api/News.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final public function listByProject($projectIdentifier, array $params = []): arr
4141
try {
4242
return $this->retrieveData('/projects/' . strval($projectIdentifier) . '/news.json', $params);
4343
} catch (SerializerException $th) {
44-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
44+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
4545
}
4646
}
4747

@@ -61,7 +61,7 @@ final public function list(array $params = []): array
6161
try {
6262
return $this->retrieveData('/news.json', $params);
6363
} catch (SerializerException $th) {
64-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
64+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
6565
}
6666
}
6767

src/Redmine/Api/Project.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final public function list(array $params = []): array
3737
try {
3838
return $this->retrieveData('/projects.json', $params);
3939
} catch (SerializerException $th) {
40-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
40+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
4141
}
4242
}
4343

@@ -224,7 +224,7 @@ final public function close($projectIdentifier): bool
224224
$lastResponse = $this->getLastResponse();
225225

226226
if ($lastResponse->getStatusCode() !== 204) {
227-
throw new UnexpectedResponseException('The Redmine server replied with the status code ' . $lastResponse->getStatusCode());
227+
throw UnexpectedResponseException::create($lastResponse);
228228
}
229229

230230
return true;
@@ -259,7 +259,7 @@ final public function reopen($projectIdentifier): bool
259259
$lastResponse = $this->getLastResponse();
260260

261261
if ($lastResponse->getStatusCode() !== 204) {
262-
throw new UnexpectedResponseException('The Redmine server replied with the status code ' . $lastResponse->getStatusCode());
262+
throw UnexpectedResponseException::create($lastResponse);
263263
}
264264

265265
return true;
@@ -294,7 +294,7 @@ final public function archive($projectIdentifier): bool
294294
$lastResponse = $this->getLastResponse();
295295

296296
if ($lastResponse->getStatusCode() !== 204) {
297-
throw new UnexpectedResponseException('The Redmine server replied with the status code ' . $lastResponse->getStatusCode());
297+
throw UnexpectedResponseException::create($lastResponse);
298298
}
299299

300300
return true;
@@ -329,7 +329,7 @@ final public function unarchive($projectIdentifier): bool
329329
$lastResponse = $this->getLastResponse();
330330

331331
if ($lastResponse->getStatusCode() !== 204) {
332-
throw new UnexpectedResponseException('The Redmine server replied with the status code ' . $lastResponse->getStatusCode());
332+
throw UnexpectedResponseException::create($lastResponse);
333333
}
334334

335335
return true;

src/Redmine/Api/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final public function list(array $params = []): array
3333
try {
3434
return $this->retrieveData('/queries.json', $params);
3535
} catch (SerializerException $th) {
36-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
36+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
3737
}
3838
}
3939

src/Redmine/Api/Role.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final public function list(array $params = []): array
3333
try {
3434
return $this->retrieveData('/roles.json', $params);
3535
} catch (SerializerException $th) {
36-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
36+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
3737
}
3838
}
3939

src/Redmine/Api/Search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final public function listByQuery(string $query, array $params = []): array
3232
try {
3333
return $this->retrieveData('/search.json', $params);
3434
} catch (SerializerException $th) {
35-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
35+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
3636
}
3737
}
3838

src/Redmine/Api/TimeEntry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final public function list(array $params = []): array
3535
try {
3636
return $this->retrieveData('/time_entries.json', $params);
3737
} catch (SerializerException $th) {
38-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
38+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
3939
}
4040
}
4141

src/Redmine/Api/TimeEntryActivity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final public function list(array $params = []): array
3131
try {
3232
return $this->retrieveData('/enumerations/time_entry_activities.json', $params);
3333
} catch (SerializerException $th) {
34-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
34+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
3535
}
3636
}
3737

src/Redmine/Api/Tracker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final public function list(array $params = []): array
3333
try {
3434
return $this->retrieveData('/trackers.json', $params);
3535
} catch (SerializerException $th) {
36-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
36+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
3737
}
3838
}
3939

src/Redmine/Api/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final public function list(array $params = []): array
3636
try {
3737
return $this->retrieveData('/users.json', $params);
3838
} catch (SerializerException $th) {
39-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
39+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
4040
}
4141
}
4242

src/Redmine/Api/Version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final public function listByProject($projectIdentifier, array $params = []): arr
4444
try {
4545
return $this->retrieveData('/projects/' . strval($projectIdentifier) . '/versions.json', $params);
4646
} catch (SerializerException $th) {
47-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
47+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
4848
}
4949
}
5050

src/Redmine/Api/Wiki.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final public function listByProject($projectIdentifier, array $params = []): arr
4444
try {
4545
return $this->retrieveData('/projects/' . strval($projectIdentifier) . '/wiki/index.json', $params);
4646
} catch (SerializerException $th) {
47-
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
47+
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
4848
}
4949
}
5050

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Redmine\Exception;
46

57
use Redmine\Exception as RedmineException;
8+
use Redmine\Http\Response;
69
use RuntimeException;
10+
use Throwable;
711

812
/**
913
* Exception if the Redmine server delivers an unexpected response.
1014
*
11-
* Use the following methods to investigate the response:
12-
*
13-
* - Redmine\Client\Client::getLastResponseStatusCode()
14-
* - Redmine\Client\Client::getLastResponseContentType()
15-
* - Redmine\Client\Client::getLastResponseBody()
15+
* Use `getResponse()` to investigate the response
1616
*/
17-
final class UnexpectedResponseException extends RuntimeException implements RedmineException {}
17+
final class UnexpectedResponseException extends RuntimeException implements RedmineException
18+
{
19+
/**
20+
* @var Response|null
21+
*/
22+
private $response = null;
23+
24+
public static function create(Response $response, Throwable $prev = null): self
25+
{
26+
$e = new self(
27+
'The Redmine server replied with an unexpected response.',
28+
($prev !== null) ? $prev->getCode() : 1,
29+
$prev
30+
);
31+
32+
$e->response = $response;
33+
34+
return $e;
35+
}
36+
37+
public function getResponse(): ?Response
38+
{
39+
return $this->response;
40+
}
41+
}

tests/Unit/Api/CustomField/ListTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function testListThrowsException()
150150
$api = new CustomField($client);
151151

152152
$this->expectException(UnexpectedResponseException::class);
153-
$this->expectExceptionMessage('The Redmine server responded with an unexpected body.');
153+
$this->expectExceptionMessage('The Redmine server replied with an unexpected response.');
154154

155155
// Perform the tests
156156
$api->list();

tests/Unit/Api/Group/ListTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testListThrowsException()
8484
$api = new Group($client);
8585

8686
$this->expectException(UnexpectedResponseException::class);
87-
$this->expectExceptionMessage('The Redmine server responded with an unexpected body.');
87+
$this->expectExceptionMessage('The Redmine server replied with an unexpected response.');
8888

8989
// Perform the tests
9090
$api->list();

tests/Unit/Api/Issue/ListTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testListThrowsException()
8484
$api = new Issue($client);
8585

8686
$this->expectException(UnexpectedResponseException::class);
87-
$this->expectExceptionMessage('The Redmine server responded with an unexpected body.');
87+
$this->expectExceptionMessage('The Redmine server replied with an unexpected response.');
8888

8989
// Perform the tests
9090
$api->list();

tests/Unit/Api/IssueCategory/ListByProjectTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function testListByProjectThrowsException()
114114
$api = new IssueCategory($client);
115115

116116
$this->expectException(UnexpectedResponseException::class);
117-
$this->expectExceptionMessage('The Redmine server responded with an unexpected body.');
117+
$this->expectExceptionMessage('The Redmine server replied with an unexpected response.');
118118

119119
// Perform the tests
120120
$api->listByProject(5);

tests/Unit/Api/IssuePriority/ListTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testListThrowsException()
8484
$api = new IssuePriority($client);
8585

8686
$this->expectException(UnexpectedResponseException::class);
87-
$this->expectExceptionMessage('The Redmine server responded with an unexpected body.');
87+
$this->expectExceptionMessage('The Redmine server replied with an unexpected response.');
8888

8989
// Perform the tests
9090
$api->list();

tests/Unit/Api/IssueRelation/ListByIssueIdTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testListByIssueIdThrowsException()
8484
$api = new IssueRelation($client);
8585

8686
$this->expectException(UnexpectedResponseException::class);
87-
$this->expectExceptionMessage('The Redmine server responded with an unexpected body.');
87+
$this->expectExceptionMessage('The Redmine server replied with an unexpected response.');
8888

8989
// Perform the tests
9090
$api->listByIssueId(5);

tests/Unit/Api/IssueStatus/ListTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testListThrowsException()
8484
$api = new IssueStatus($client);
8585

8686
$this->expectException(UnexpectedResponseException::class);
87-
$this->expectExceptionMessage('The Redmine server responded with an unexpected body.');
87+
$this->expectExceptionMessage('The Redmine server replied with an unexpected response.');
8888

8989
// Perform the tests
9090
$api->list();

tests/Unit/Api/Membership/ListByProjectTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function testListByProjectThrowsException()
112112
$api = new Membership($client);
113113

114114
$this->expectException(UnexpectedResponseException::class);
115-
$this->expectExceptionMessage('The Redmine server responded with an unexpected body.');
115+
$this->expectExceptionMessage('The Redmine server replied with an unexpected response.');
116116

117117
// Perform the tests
118118
$api->listByProject(5);

tests/Unit/Api/News/ListByProjectTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function testListByProjectThrowsException()
114114
$api = new News($client);
115115

116116
$this->expectException(UnexpectedResponseException::class);
117-
$this->expectExceptionMessage('The Redmine server responded with an unexpected body.');
117+
$this->expectExceptionMessage('The Redmine server replied with an unexpected response.');
118118

119119
// Perform the tests
120120
$api->listByProject(5);

tests/Unit/Api/News/ListTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testListThrowsException()
8484
$api = new News($client);
8585

8686
$this->expectException(UnexpectedResponseException::class);
87-
$this->expectExceptionMessage('The Redmine server responded with an unexpected body.');
87+
$this->expectExceptionMessage('The Redmine server replied with an unexpected response.');
8888

8989
// Perform the tests
9090
$api->list();

tests/Unit/Api/Project/ArchiveTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testArchiveThrowsUnexpectedResponseException()
5050
$api = new Project($client);
5151

5252
$this->expectException(UnexpectedResponseException::class);
53-
$this->expectExceptionMessage('The Redmine server replied with the status code 403');
53+
$this->expectExceptionMessage('The Redmine server replied with an unexpected response.');
5454

5555
$api->archive(5);
5656
}

tests/Unit/Api/Project/CloseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function testCloseThrowsUnexpectedResponseException()
4949
$api = new Project($client);
5050

5151
$this->expectException(UnexpectedResponseException::class);
52-
$this->expectExceptionMessage('The Redmine server replied with the status code 403');
52+
$this->expectExceptionMessage('The Redmine server replied with an unexpected response.');
5353

5454
$api->close(5);
5555
}

tests/Unit/Api/Project/ListTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testListThrowsException()
8484
$api = new Project($client);
8585

8686
$this->expectException(UnexpectedResponseException::class);
87-
$this->expectExceptionMessage('The Redmine server responded with an unexpected body.');
87+
$this->expectExceptionMessage('The Redmine server replied with an unexpected response.');
8888

8989
// Perform the tests
9090
$api->list();

0 commit comments

Comments
 (0)