Skip to content

Add new method UnexpectedResponseException::getResponse() #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New method `Redmine\Api\Project::reopen()` to reopen a project.
- New method `Redmine\Api\Project::archive()` to archive a project.
- New method `Redmine\Api\Project::unarchive()` to unarchive a project.
- New method `UnexpectedResponseException::getResponse()` to get the last response responsible for the exception.

### Changed

Expand All @@ -35,7 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New method `Redmine\Api\News::list()` to list news from all project.
- New method `Redmine\Api\News::listByProject()` to list news from a project.
- New method `Redmine\Api\Project::list()` to list projects.
- New method `Redmine\Api\Query::list()` to list projects.
- New method `Redmine\Api\Query::list()` to list queries.
- New method `Redmine\Api\Role::list()` to list roles.
- New method `Redmine\Api\Search::listByQuery()` to list search results by query.
- New method `Redmine\Api\TimeEntry::list()` to list time entries.
Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final public function list(array $params = []): array
try {
return $this->retrieveData('/custom_fields.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final public function list(array $params = []): array
try {
return $this->retrieveData('/groups.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ final public function list(array $params = []): array
try {
return $this->retrieveData('/issues.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/IssueCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final public function listByProject($projectIdentifier, array $params = []): arr
try {
return $this->retrieveData('/projects/' . strval($projectIdentifier) . '/issue_categories.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/IssuePriority.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final public function list(array $params = []): array
try {
return $this->retrieveData('/enumerations/issue_priorities.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/IssueRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final public function listByIssueId(int $issueId, array $params = []): array
try {
return $this->retrieveData('/issues/' . strval($issueId) . '/relations.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/IssueStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final public function list(array $params = []): array
try {
return $this->retrieveData('/issue_statuses.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final public function listByProject($projectIdentifier, array $params = []): arr
try {
return $this->retrieveData('/projects/' . strval($projectIdentifier) . '/memberships.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Redmine/Api/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final public function listByProject($projectIdentifier, array $params = []): arr
try {
return $this->retrieveData('/projects/' . strval($projectIdentifier) . '/news.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

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

Expand Down
10 changes: 5 additions & 5 deletions src/Redmine/Api/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final public function list(array $params = []): array
try {
return $this->retrieveData('/projects.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

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

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

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

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

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

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

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

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

return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final public function list(array $params = []): array
try {
return $this->retrieveData('/queries.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final public function list(array $params = []): array
try {
return $this->retrieveData('/roles.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final public function listByQuery(string $query, array $params = []): array
try {
return $this->retrieveData('/search.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/TimeEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final public function list(array $params = []): array
try {
return $this->retrieveData('/time_entries.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/TimeEntryActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final public function list(array $params = []): array
try {
return $this->retrieveData('/enumerations/time_entry_activities.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final public function list(array $params = []): array
try {
return $this->retrieveData('/trackers.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final public function list(array $params = []): array
try {
return $this->retrieveData('/users.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final public function listByProject($projectIdentifier, array $params = []): arr
try {
return $this->retrieveData('/projects/' . strval($projectIdentifier) . '/versions.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Wiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final public function listByProject($projectIdentifier, array $params = []): arr
try {
return $this->retrieveData('/projects/' . strval($projectIdentifier) . '/wiki/index.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}

Expand Down
36 changes: 30 additions & 6 deletions src/Redmine/Exception/UnexpectedResponseException.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
<?php

declare(strict_types=1);

namespace Redmine\Exception;

use Redmine\Exception as RedmineException;
use Redmine\Http\Response;
use RuntimeException;
use Throwable;

/**
* Exception if the Redmine server delivers an unexpected response.
*
* Use the following methods to investigate the response:
*
* - Redmine\Client\Client::getLastResponseStatusCode()
* - Redmine\Client\Client::getLastResponseContentType()
* - Redmine\Client\Client::getLastResponseBody()
* Use `getResponse()` to investigate the response
*/
final class UnexpectedResponseException extends RuntimeException implements RedmineException {}
final class UnexpectedResponseException extends RuntimeException implements RedmineException
{
/**
* @var Response|null
*/
private $response = null;

public static function create(Response $response, Throwable $prev = null): self
{
$e = new self(
'The Redmine server replied with an unexpected response.',
($prev !== null) ? $prev->getCode() : 1,
$prev
);

$e->response = $response;

return $e;
}

public function getResponse(): ?Response
{
return $this->response;
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Api/CustomField/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function testListThrowsException()
$api = new CustomField($client);

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

// Perform the tests
$api->list();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/Group/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testListThrowsException()
$api = new Group($client);

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

// Perform the tests
$api->list();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/Issue/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testListThrowsException()
$api = new Issue($client);

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

// Perform the tests
$api->list();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/IssueCategory/ListByProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function testListByProjectThrowsException()
$api = new IssueCategory($client);

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

// Perform the tests
$api->listByProject(5);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/IssuePriority/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testListThrowsException()
$api = new IssuePriority($client);

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

// Perform the tests
$api->list();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/IssueRelation/ListByIssueIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testListByIssueIdThrowsException()
$api = new IssueRelation($client);

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

// Perform the tests
$api->listByIssueId(5);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/IssueStatus/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testListThrowsException()
$api = new IssueStatus($client);

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

// Perform the tests
$api->list();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/Membership/ListByProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function testListByProjectThrowsException()
$api = new Membership($client);

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

// Perform the tests
$api->listByProject(5);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/News/ListByProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function testListByProjectThrowsException()
$api = new News($client);

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

// Perform the tests
$api->listByProject(5);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/News/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testListThrowsException()
$api = new News($client);

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

// Perform the tests
$api->list();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/Project/ArchiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testArchiveThrowsUnexpectedResponseException()
$api = new Project($client);

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

$api->archive(5);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/Project/CloseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testCloseThrowsUnexpectedResponseException()
$api = new Project($client);

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

$api->close(5);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/Project/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testListThrowsException()
$api = new Project($client);

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

// Perform the tests
$api->list();
Expand Down
Loading