Skip to content

Commit 411afef

Browse files
committed
Exception handler return response object.
1 parent 3859cdd commit 411afef

10 files changed

+262
-172
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
"plaisio/console": "^2.0.1",
1515
"plaisio/error-logger": "^1.2",
1616
"plaisio/exception": "^1.1",
17-
"plaisio/exception-handler": "^1.2",
17+
"plaisio/exception-handler": "^2.0",
1818
"plaisio/kernel": "^3.1.1",
1919
"plaisio/obfuscator": "^2.0",
2020
"plaisio/request": "^0.11",
2121
"plaisio/request-logger": "^1.2",
22-
"plaisio/response-core": "^1.1 || ^2.0",
22+
"plaisio/response-core": "^2.0",
2323
"plaisio/session": "^4.0",
2424
"setbased/helper-code-store-php": "^2.4.1",
2525
"setbased/php-stratum-middle": "^5.2",

src/BadRequestExceptionAgent.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Plaisio\Exception\BadRequestException;
77
use Plaisio\PlaisioObject;
88
use Plaisio\Response\BadRequestResponse;
9+
use Plaisio\Response\Response;
910

1011
/**
1112
* An agent that handles BadRequestException exceptions.
@@ -18,12 +19,14 @@ class BadRequestExceptionAgent extends PlaisioObject
1819
*
1920
* @param BadRequestException $exception The exception.
2021
*
22+
* @return Response
23+
*
2124
* @since 1.0.0
2225
* @api
2326
*/
24-
public function handleConstructException(BadRequestException $exception): void
27+
public function handleConstructException(BadRequestException $exception): Response
2528
{
26-
$this->handleException($exception);
29+
return $this->handleException($exception);
2730
}
2831

2932
//--------------------------------------------------------------------------------------------------------------------
@@ -32,12 +35,14 @@ public function handleConstructException(BadRequestException $exception): void
3235
*
3336
* @param BadRequestException $exception The exception.
3437
*
38+
* @return Response
39+
*
3540
* @since 1.0.0
3641
* @api
3742
*/
38-
public function handlePrepareException(BadRequestException $exception): void
43+
public function handlePrepareException(BadRequestException $exception): Response
3944
{
40-
$this->handleException($exception);
45+
return $this->handleException($exception);
4146
}
4247

4348
//--------------------------------------------------------------------------------------------------------------------
@@ -46,27 +51,30 @@ public function handlePrepareException(BadRequestException $exception): void
4651
*
4752
* @param BadRequestException $exception The exception.
4853
*
54+
* @return Response
55+
*
4956
* @since 1.0.0
5057
* @api
5158
*/
52-
public function handleResponseException(BadRequestException $exception): void
59+
public function handleResponseException(BadRequestException $exception): Response
5360
{
54-
$this->handleException($exception);
61+
return $this->handleException($exception);
5562
}
5663

5764
//--------------------------------------------------------------------------------------------------------------------
5865
/**
5966
* Handles a BadRequestException.
6067
*
6168
* @param BadRequestException $exception The exception.
69+
*
70+
* @return Response
6271
*/
63-
private function handleException(BadRequestException $exception): void
72+
private function handleException(BadRequestException $exception): Response
6473
{
6574
$this->nub->DL->rollback();
6675

6776
// Set the HTTP status to 400 (Bad Request).
6877
$response = new BadRequestResponse();
69-
$response->send();
7078

7179
// Log the bad request.
7280
$this->nub->requestLogger->logRequest($response->getStatus());
@@ -77,6 +85,8 @@ private function handleException(BadRequestException $exception): void
7785
{
7886
$this->nub->errorLogger->logError($exception);
7987
}
88+
89+
return $response;
8090
}
8191

8292
//--------------------------------------------------------------------------------------------------------------------

src/DecodeExceptionAgent.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Plaisio\Obfuscator\Exception\DecodeException;
77
use Plaisio\PlaisioObject;
88
use Plaisio\Response\BadRequestResponse;
9+
use Plaisio\Response\Response;
910

1011
/**
1112
* An agent that handles DecodeException exceptions.
@@ -18,12 +19,14 @@ class DecodeExceptionAgent extends PlaisioObject
1819
*
1920
* @param DecodeException $exception The exception.
2021
*
22+
* @return Response
23+
*
2124
* @since 1.2.0
2225
* @api
2326
*/
24-
public function handleConstructException(DecodeException $exception): void
27+
public function handleConstructException(DecodeException $exception): Response
2528
{
26-
$this->handleException($exception);
29+
return $this->handleException($exception);
2730
}
2831

2932
//--------------------------------------------------------------------------------------------------------------------
@@ -32,12 +35,14 @@ public function handleConstructException(DecodeException $exception): void
3235
*
3336
* @param DecodeException $exception The exception.
3437
*
38+
* @return Response
39+
*
3540
* @since 1.2.0
3641
* @api
3742
*/
38-
public function handlePrepareException(DecodeException $exception): void
43+
public function handlePrepareException(DecodeException $exception): Response
3944
{
40-
$this->handleException($exception);
45+
return $this->handleException($exception);
4146
}
4247

4348
//--------------------------------------------------------------------------------------------------------------------
@@ -46,27 +51,30 @@ public function handlePrepareException(DecodeException $exception): void
4651
*
4752
* @param DecodeException $exception The exception.
4853
*
54+
* @return Response
55+
*
4956
* @since 1.2.0
5057
* @api
5158
*/
52-
public function handleResponseException(DecodeException $exception): void
59+
public function handleResponseException(DecodeException $exception): Response
5360
{
54-
$this->handleException($exception);
61+
return $this->handleException($exception);
5562
}
5663

5764
//--------------------------------------------------------------------------------------------------------------------
5865
/**
5966
* Handles a DecodeException.
6067
*
6168
* @param DecodeException $exception The exception.
69+
*
70+
* @return Response
6271
*/
63-
private function handleException(DecodeException $exception): void
72+
private function handleException(DecodeException $exception): Response
6473
{
6574
$this->nub->DL->rollback();
6675

6776
// Set the HTTP status to 400 (Bad Request).
6877
$response = new BadRequestResponse();
69-
$response->send();
7078

7179
// Log the bad request.
7280
$this->nub->requestLogger->logRequest($response->getStatus());
@@ -77,6 +85,8 @@ private function handleException(DecodeException $exception): void
7785
{
7886
$this->nub->errorLogger->logError($exception);
7987
}
88+
89+
return $response;
8090
}
8191

8292
//--------------------------------------------------------------------------------------------------------------------

src/Helper/ExceptionHandlerCodeGenerator.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Plaisio\ExceptionHandler\ExceptionHandler;
77
use Plaisio\PlaisioObject;
8+
use Plaisio\Response\Response;
89
use SetBased\Helper\CodeStore\Importing;
910
use SetBased\Helper\CodeStore\PhpCodeStore;
1011

@@ -55,6 +56,7 @@ public function generateCode(string $fullyQualifiedName, array $allAgents): stri
5556
$this->importing = new Importing($namespace);
5657
$this->importing->addClass(ExceptionHandler::class);
5758
$this->importing->addClass(PlaisioObject::class);
59+
$this->importing->addClass(Response::class);
5860
$this->importClasses($allAgents);
5961

6062
$this->importing->prepare();
@@ -121,30 +123,38 @@ private function generateMethod(string $method, array $agents)
121123
$this->store->append('/**');
122124
$this->store->append(' * @inheritdoc', false);
123125
$this->store->append(' */', false);
124-
$this->store->append(sprintf('public function %s(%s $exception): void',
126+
$this->store->append(sprintf('public function %s(%s $exception): Response',
125127
$method,
126128
$this->importing->simplyFullyQualifiedName('\\Throwable')));
127129
$this->store->append('{');
128-
$this->store->append('switch (true)');
129-
$this->store->append('{');
130130
$first = true;
131131
foreach ($agents as $agent)
132132
{
133133
if (!$first) $this->store->append('');
134+
$isThrowable = ($this->importing->simplyFullyQualifiedName($agent['type'])==='\Throwable');
135+
136+
if (!$isThrowable)
137+
{
138+
$this->store->append(sprintf("if (is_a(\$exception, %s::class))",
139+
$this->importing->simplyFullyQualifiedName($agent['type'])));
140+
$this->store->append('{');
141+
$this->store->append(sprintf('/** @var %s $exception */',
142+
$this->importing->simplyFullyQualifiedName($agent['type'])));
143+
}
134144

135-
$this->store->append(sprintf("case is_a(\$exception, %s::class):",
136-
$this->importing->simplyFullyQualifiedName($agent['type'])));
137-
$this->store->append(sprintf('/** @var %s $exception */',
138-
$this->importing->simplyFullyQualifiedName($agent['type'])));
139145
$this->store->append(sprintf("\$handler = new %s(\$this);",
140146
$this->importing->simplyFullyQualifiedName($agent['class'])));
141-
$this->store->append(sprintf('$handler->%s($exception);', $agent['method']));
142-
$this->store->append('break;');
147+
$this->store->append('');
148+
$this->store->append(sprintf('return $handler->%s($exception);', $agent['method']));
149+
150+
if (!$isThrowable)
151+
{
152+
$this->store->append('}');
153+
}
143154

144155
$first = false;
145156
}
146157
$this->store->append('}');
147-
$this->store->append('}');
148158
$this->store->append('');
149159
}
150160

src/InvalidUrlExceptionAgent.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Plaisio\Exception\InvalidUrlException;
77
use Plaisio\PlaisioObject;
88
use Plaisio\Response\NotFoundResponse;
9+
use Plaisio\Response\Response;
910

1011
/**
1112
* An agent that handles InvalidUrlException exceptions.
@@ -18,12 +19,14 @@ class InvalidUrlExceptionAgent extends PlaisioObject
1819
*
1920
* @param InvalidUrlException $exception The exception.
2021
*
22+
* @return Response
23+
*
2124
* @since 1.0.0
2225
* @api
2326
*/
24-
public function handleConstructException(InvalidUrlException $exception): void
27+
public function handleConstructException(InvalidUrlException $exception): Response
2528
{
26-
$this->handleException($exception);
29+
return $this->handleException($exception);
2730
}
2831

2932
//--------------------------------------------------------------------------------------------------------------------
@@ -32,12 +35,14 @@ public function handleConstructException(InvalidUrlException $exception): void
3235
*
3336
* @param InvalidUrlException $exception The exception.
3437
*
38+
* @return Response
39+
*
3540
* @since 1.0.0
3641
* @api
3742
*/
38-
public function handlePrepareException(InvalidUrlException $exception): void
43+
public function handlePrepareException(InvalidUrlException $exception): Response
3944
{
40-
$this->handleException($exception);
45+
return $this->handleException($exception);
4146
}
4247

4348
//--------------------------------------------------------------------------------------------------------------------
@@ -46,27 +51,30 @@ public function handlePrepareException(InvalidUrlException $exception): void
4651
*
4752
* @param InvalidUrlException $exception The exception.
4853
*
54+
* @return Response
55+
*
4956
* @since 1.0.0
5057
* @api
5158
*/
52-
public function handleResponseException(InvalidUrlException $exception): void
59+
public function handleResponseException(InvalidUrlException $exception): Response
5360
{
54-
$this->handleException($exception);
61+
return $this->handleException($exception);
5562
}
5663

5764
//--------------------------------------------------------------------------------------------------------------------
5865
/**
5966
* Handles an InvalidUrlException.
6067
*
6168
* @param InvalidUrlException $exception The exception.
69+
*
70+
* @return Response
6271
*/
63-
private function handleException(InvalidUrlException $exception): void
72+
private function handleException(InvalidUrlException $exception): Response
6473
{
6574
$this->nub->DL->rollback();
6675

6776
// Set the HTTP status to 404 (Not Found).
6877
$response = new NotFoundResponse();
69-
$response->send();
7078

7179
// Log the invalid request request.
7280
$this->nub->requestLogger->logRequest($response->getStatus());
@@ -77,6 +85,8 @@ private function handleException(InvalidUrlException $exception): void
7785
{
7886
$this->nub->errorLogger->logError($exception);
7987
}
88+
89+
return $response;
8090
}
8191

8292
//--------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)