Skip to content

Commit f3a21a5

Browse files
authored
fix(core): register HttpExceptionHandler only in production (#1220)
1 parent 98bfc5f commit f3a21a5

File tree

3 files changed

+25
-37
lines changed

3 files changed

+25
-37
lines changed

packages/console/src/Testing/ConsoleTester.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ public function call(string|Closure|array $command, string|array $arguments = []
7575

7676
$clone->container->singleton(Console::class, $console);
7777

78-
$appConfig = $this->container->get(AppConfig::class);
79-
$appConfig->exceptionProcessors[] = $clone->container->get(ConsoleExceptionHandler::class);
80-
8178
$clone->output = $memoryOutputBuffer;
8279
$clone->input = $memoryInputBuffer;
8380

packages/core/src/ExceptionHandlerInitializer.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/core/src/FrameworkKernel.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tempest\Core;
66

77
use Dotenv\Dotenv;
8+
use Tempest\Console\Exceptions\ConsoleExceptionHandler;
89
use Tempest\Container\Container;
910
use Tempest\Container\GenericContainer;
1011
use Tempest\Core\Kernel\FinishDeferredTasks;
@@ -14,6 +15,7 @@
1415
use Tempest\Core\Kernel\RegisterEmergencyExceptionHandler;
1516
use Tempest\Core\ShellExecutors\GenericShellExecutor;
1617
use Tempest\EventBus\EventBus;
18+
use Tempest\Router\Exceptions\HttpExceptionHandler;
1719

1820
final class FrameworkKernel implements Kernel
1921
{
@@ -50,15 +52,15 @@ public static function boot(
5052
)
5153
->validateRoot()
5254
->loadEnv()
53-
->registerEmergencyErrorHandler()
55+
->registerEmergencyExceptionHandler()
5456
->registerShutdownFunction()
5557
->registerInternalStorage()
5658
->registerKernel()
5759
->loadComposer()
5860
->loadDiscoveryLocations()
5961
->loadConfig()
6062
->loadDiscovery()
61-
->registerErrorHandler()
63+
->registerExceptionHandler()
6264
->event(KernelEvent::BOOTED);
6365
}
6466

@@ -190,7 +192,7 @@ public function event(object $event): self
190192
return $this;
191193
}
192194

193-
public function registerEmergencyErrorHandler(): self
195+
public function registerEmergencyExceptionHandler(): self
194196
{
195197
$environment = Environment::fromEnv();
196198

@@ -208,7 +210,7 @@ public function registerEmergencyErrorHandler(): self
208210
return $this;
209211
}
210212

211-
public function registerErrorHandler(): self
213+
public function registerExceptionHandler(): self
212214
{
213215
$appConfig = $this->container->get(AppConfig::class);
214216

@@ -217,16 +219,25 @@ public function registerErrorHandler(): self
217219
return $this;
218220
}
219221

220-
$handler = $this->container->get(ExceptionHandler::class);
221-
set_exception_handler($handler->handle(...));
222-
set_error_handler(fn (int $code, string $message, string $filename, int $line) => $handler->handle(
223-
new \ErrorException(
224-
message: $message,
225-
code: $code,
226-
filename: $filename,
227-
line: $line,
228-
),
229-
));
222+
// We need an exception handler for the CLI in every
223+
// environment, and one for HTTP only in production.
224+
$handler = match (true) {
225+
PHP_SAPI === 'cli' => $this->container->get(ConsoleExceptionHandler::class),
226+
$appConfig->environment->isProduction() => $this->container->get(HttpExceptionHandler::class),
227+
default => null,
228+
};
229+
230+
if ($handler) {
231+
set_exception_handler($handler->handle(...));
232+
set_error_handler(fn (int $code, string $message, string $filename, int $line) => $handler->handle(
233+
new \ErrorException(
234+
message: $message,
235+
code: $code,
236+
filename: $filename,
237+
line: $line,
238+
),
239+
));
240+
}
230241

231242
return $this;
232243
}

0 commit comments

Comments
 (0)