fedi-feed-router/backend/app/Providers/AppServiceProvider.php

51 lines
1.3 KiB
PHP
Raw Normal View History

2025-06-29 08:50:03 +02:00
<?php
namespace App\Providers;
2025-08-15 16:39:18 +02:00
use Domains\Logging\Enums\LogLevelEnum;
use Domains\Logging\Events\ExceptionOccurred;
use Domains\Logging\Listeners\LogExceptionToDatabase;
2025-07-05 18:26:04 +02:00
use Error;
2025-06-29 18:33:18 +02:00
use Illuminate\Contracts\Debug\ExceptionHandler;
2025-06-29 09:48:45 +02:00
use Illuminate\Support\Facades\Event;
2025-06-29 08:50:03 +02:00
use Illuminate\Support\ServiceProvider;
2025-07-05 18:26:04 +02:00
use InvalidArgumentException;
2025-06-29 18:33:18 +02:00
use Throwable;
2025-06-29 08:50:03 +02:00
class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
}
public function boot(): void
{
2025-06-29 18:33:18 +02:00
Event::listen(
ExceptionOccurred::class,
LogExceptionToDatabase::class,
);
2025-08-03 20:59:09 +02:00
Event::listen(
2025-08-15 16:39:18 +02:00
\Domains\Article\Events\NewArticleFetched::class,
\Domains\Article\Listeners\ValidateArticleListener::class,
2025-08-03 20:59:09 +02:00
);
2025-06-29 18:33:18 +02:00
app()->make(ExceptionHandler::class)
->reportable(function (Throwable $e) {
$level = $this->mapExceptionToLogLevel($e);
ExceptionOccurred::dispatch($e, $level, $e->getMessage(), []);
});
}
private function mapExceptionToLogLevel(Throwable $exception): LogLevelEnum
{
return match (true) {
2025-07-05 18:26:04 +02:00
$exception instanceof Error => LogLevelEnum::CRITICAL,
$exception instanceof InvalidArgumentException => LogLevelEnum::WARNING,
2025-06-29 18:33:18 +02:00
default => LogLevelEnum::ERROR,
};
2025-06-29 08:50:03 +02:00
}
}