Skip to content

Commit 2e9b269

Browse files
feat(error handling): Use a specific BouncerException that extends RuntimeException
1 parent 764eab7 commit 2e9b269

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/StandAloneBounce.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace CrowdSecBouncer;
44

5-
use ErrorException;
65
use Symfony\Component\Cache\Adapter\AbstractAdapter;
76
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
87
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
@@ -122,7 +121,7 @@ public function getBouncerInstance(): Bouncer
122121
$maxRemediationLevel = Constants::REMEDIATION_BAN;
123122
break;
124123
default:
125-
throw new Exception("Unknown $bouncingLevel");
124+
throw new \Exception("Unknown $bouncingLevel");
126125
}
127126

128127
// Instanciate the bouncer
@@ -344,7 +343,7 @@ public function sendResponse(?string $body, int $statusCode = 200): void
344343
header('Pragma: no-cache');
345344
break;
346345
default:
347-
throw new Exception("Unhandled code ${statusCode}");
346+
throw new \Exception("Unhandled code ${statusCode}");
348347
}
349348
if (null !== $body) {
350349
echo $body;
@@ -354,10 +353,10 @@ public function sendResponse(?string $body, int $statusCode = 200): void
354353

355354
public function safelyBounce(): void
356355
{
357-
// If there is any technical problem while bouncing, don't block the user. Bypass boucing and log the error.
356+
// If there is any technical problem while bouncing, don't block the user. Bypass bouncing and log the error.
358357
try {
359-
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
360-
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
358+
set_error_handler(function ($errno, $errstr) {
359+
throw new BouncerException("$errstr (Error level: $errno)");
361360
});
362361
$this->run();
363362
restore_error_handler();

0 commit comments

Comments
 (0)