Skip to content

Commit a830568

Browse files
authored
[Feature] Update exception hierarchy (#8)
* Update exception hierarchy
1 parent cd8cc8b commit a830568

11 files changed

+47
-8
lines changed

CHANGELOG.MD

+5
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@ v0.1.2
1616
* [Feature] Add rule "OnTargetBranchRule"
1717
* Add property "sourceBranch" to MergeRequest object
1818
* Add property "targetBranch" to MergeRequest object
19+
20+
v0.1.3
21+
--------------------
22+
* [Feature] Update exception hierarchy. Parent: MergeRequestLinterException
23+
* [Feature] Add ServerUnexpectedResponseException

src/Ci/System/InteractsWithResponse.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ArtARTs36\MergeRequestLinter\Ci\System;
44

55
use ArtARTs36\MergeRequestLinter\Exception\InvalidCredentialsException;
6+
use ArtARTs36\MergeRequestLinter\Exception\ServerUnexpectedResponseException;
67
use Psr\Http\Message\ResponseInterface;
78

89
trait InteractsWithResponse
@@ -14,9 +15,13 @@ trait InteractsWithResponse
1415
protected function validateResponse(ResponseInterface $response, string $ciName): void
1516
{
1617
if ($response->getStatusCode() === 401 || $response->getStatusCode() === 403) {
17-
throw InvalidCredentialsException::fromCiName($ciName);
18+
throw InvalidCredentialsException::fromResponse($ciName, $response->getBody()->getContents());
1819
} elseif ($response->getStatusCode() !== 200) {
19-
throw new \RuntimeException($ciName . ' returns response with code '. $response->getStatusCode());
20+
throw ServerUnexpectedResponseException::create(
21+
$ciName,
22+
$response->getStatusCode(),
23+
$response->getBody()->getContents()
24+
);
2025
}
2126
}
2227

src/Ci/System/SystemFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function create(string $ciName): CiSystem
4040
$targetClass = $this->ciMap[$ciName] ?? null;
4141

4242
if ($targetClass === null) {
43-
throw new CiNotSupported();
43+
throw CiNotSupported::fromCiName($ciName);
4444
}
4545

4646
if (! $this->config->getCredentials()->has($targetClass)) {

src/Contracts/CiSystem.php

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ArtARTs36\MergeRequestLinter\Contracts;
44

55
use ArtARTs36\MergeRequestLinter\Exception\InvalidCredentialsException;
6+
use ArtARTs36\MergeRequestLinter\Exception\ServerUnexpectedResponseException;
67
use ArtARTs36\MergeRequestLinter\Request\MergeRequest;
78

89
/**
@@ -23,6 +24,7 @@ public function isMergeRequest(): bool;
2324
/**
2425
* Get current merge request
2526
* @throws InvalidCredentialsException
27+
* @throws ServerUnexpectedResponseException
2628
*/
2729
public function getMergeRequest(): MergeRequest;
2830
}

src/Exception/CiNotSupported.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace ArtARTs36\MergeRequestLinter\Exception;
44

5-
class CiNotSupported extends \RuntimeException
5+
class CiNotSupported extends MergeRequestLinterException
66
{
7-
//
7+
public static function fromCiName(string $ciName): self
8+
{
9+
return new self('CI "' . $ciName . '" not supported');
10+
}
811
}

src/Exception/ConfigInvalidException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ArtARTs36\MergeRequestLinter\Exception;
44

5-
class ConfigInvalidException extends \RuntimeException
5+
class ConfigInvalidException extends MergeRequestLinterException
66
{
77
public static function fromKey(string $key): self
88
{

src/Exception/EnvironmentDataKeyNotFound.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Throwable;
66

7-
class EnvironmentDataKeyNotFound extends \RuntimeException
7+
class EnvironmentDataKeyNotFound extends MergeRequestLinterException
88
{
99
public function __construct(protected string $dataKey, int $code = 0, Throwable $previous = null)
1010
{

src/Exception/InvalidCredentialsException.php

+5
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ public static function fromCiName(string $ciName): self
88
{
99
return new self("Given invalid credentials for $ciName");
1010
}
11+
12+
public static function fromResponse(string $ciName, string $response): self
13+
{
14+
return new self("Given invalid credentials for $ciName. Server returns: ". $response);
15+
}
1116
}

src/Exception/LintException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ArtARTs36\MergeRequestLinter\Exception;
44

5-
abstract class LintException extends \Exception
5+
abstract class LintException extends MergeRequestLinterException
66
{
77
//
88
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Exception;
4+
5+
abstract class MergeRequestLinterException extends \RuntimeException
6+
{
7+
//
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Exception;
4+
5+
final class ServerUnexpectedResponseException extends MergeRequestLinterException
6+
{
7+
public static function create(string $ciName, int $status, string $response): self
8+
{
9+
return new self($ciName . ' returns response with code ' . $status . '. Response: ' . $response);
10+
}
11+
}

0 commit comments

Comments
 (0)