Skip to content

Commit bb0a483

Browse files
committed
Add DecodeExceptionAgent.
1 parent 5541bc3 commit bb0a483

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"plaisio/exception": "^1.1",
1616
"plaisio/exception-handler": "^1.1",
1717
"plaisio/kernel": "^1.1",
18+
"plaisio/obfuscator": "^1.2",
1819
"plaisio/response-core": "^1.1",
1920
"setbased/helper-code-store-php": "^2.3",
2021
"setbased/php-stratum-middle": "^5.2",

src/DecodeExceptionAgent.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Plaisio\ExceptionHandler;
5+
6+
use Plaisio\Kernel\Nub;
7+
use Plaisio\Obfuscator\Exception\DecodeException;
8+
use Plaisio\Response\BadRequestResponse;
9+
10+
/**
11+
* An agent that handles DecodeException exceptions.
12+
*/
13+
class DecodeExceptionAgent
14+
{
15+
//--------------------------------------------------------------------------------------------------------------------
16+
/**
17+
* Handles a DecodeException thrown in the constructor of a page object.
18+
*
19+
* @param DecodeException $exception The exception.
20+
*
21+
* @since 1.2.0
22+
* @api
23+
*/
24+
public function handleConstructException(DecodeException $exception): void
25+
{
26+
$this->handleException($exception);
27+
}
28+
29+
//--------------------------------------------------------------------------------------------------------------------
30+
/**
31+
* Handles a DecodeException thrown during generating the response by a page object.
32+
*
33+
* @param DecodeException $exception The exception.
34+
*
35+
* @since 1.2.0
36+
* @api
37+
*/
38+
public function handleResponseException(DecodeException $exception): void
39+
{
40+
$this->handleException($exception);
41+
}
42+
43+
//--------------------------------------------------------------------------------------------------------------------
44+
/**
45+
* Handles a DecodeException.
46+
*
47+
* @param DecodeException $exception The exception.
48+
*/
49+
private function handleException(DecodeException $exception): void
50+
{
51+
Nub::$DL->rollback();
52+
53+
// Set the HTTP status to 400 (Bad Request).
54+
$response = new BadRequestResponse();
55+
$response->send();
56+
57+
// Log the bad request.
58+
Nub::$requestLogger->logRequest($response->getStatus());
59+
Nub::$DL->commit();
60+
61+
// Only on development environment log the error.
62+
if (Nub::$request->isEnvDev())
63+
{
64+
$logger = Nub::$nub->getErrorLogger();
65+
$logger->logError($exception);
66+
}
67+
}
68+
69+
//--------------------------------------------------------------------------------------------------------------------
70+
}
71+
72+
//----------------------------------------------------------------------------------------------------------------------

test/Command/Foo.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ class Foo implements ExceptionHandler
5858
$handler->handleConstructException($exception);
5959
break;
6060

61+
case is_a($exception, 'Plaisio\Obfuscator\Exception\DecodeException'):
62+
/** @var \Plaisio\Obfuscator\Exception\DecodeException $exception */
63+
$handler = new \Plaisio\ExceptionHandler\DecodeExceptionAgent();
64+
$handler->handleConstructException($exception);
65+
break;
66+
6167
case is_a($exception, 'Plaisio\Exception\InvalidUrlException'):
6268
/** @var \Plaisio\Exception\InvalidUrlException $exception */
6369
$handler = new \Plaisio\ExceptionHandler\InvalidUrlExceptionAgent();
@@ -98,6 +104,12 @@ class Foo implements ExceptionHandler
98104
$handler->handleResponseException($exception);
99105
break;
100106

107+
case is_a($exception, 'Plaisio\Obfuscator\Exception\DecodeException'):
108+
/** @var \Plaisio\Obfuscator\Exception\DecodeException $exception */
109+
$handler = new \Plaisio\ExceptionHandler\DecodeExceptionAgent();
110+
$handler->handleResponseException($exception);
111+
break;
112+
101113
case is_a($exception, 'Plaisio\Exception\InvalidUrlException'):
102114
/** @var \Plaisio\Exception\InvalidUrlException $exception */
103115
$handler = new \Plaisio\ExceptionHandler\InvalidUrlExceptionAgent();

test/Command/plaisio.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
<class>\SetBased\Stratum\Test\Application\Foo</class>
44
<path>test/Command/Foo.php</path>
55
<agents>
6-
<agent>\Plaisio\ExceptionHandler\ThrowableAgent</agent>
6+
<agent>\Plaisio\ExceptionHandler\BadRequestExceptionAgent</agent>
7+
<agent>\Plaisio\ExceptionHandler\DecodeExceptionAgent</agent>
78
<agent>\Plaisio\ExceptionHandler\InvalidUrlExceptionAgent</agent>
89
<agent>\Plaisio\ExceptionHandler\NotAuthorizedExceptionAgent</agent>
910
<agent>\Plaisio\ExceptionHandler\NotPreferredUrlExceptionAgent</agent>
10-
<agent>\Plaisio\ExceptionHandler\BadRequestExceptionAgent</agent>
1111
<agent>\Plaisio\ExceptionHandler\ResultExceptionAgent</agent>
12+
<agent>\Plaisio\ExceptionHandler\ThrowableAgent</agent>
1213
</agents>
1314
</exception>
1415
</plaisio>

0 commit comments

Comments
 (0)