fedi-feed-router/backend/app/Providers/AppServiceProvider.php
myrmidex d2416a3ae2 Fix tests
+ Move LogLevelEnum
2025-08-05 21:27:42 +02:00

60 lines
1.6 KiB
PHP

<?php
namespace App\Providers;
use App\Enums\LogLevelEnum;
use App\Events\ExceptionOccurred;
use App\Listeners\LogExceptionToDatabase;
use Error;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use InvalidArgumentException;
use Throwable;
class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
}
public function boot(): void
{
Event::listen(
ExceptionOccurred::class,
LogExceptionToDatabase::class,
);
Event::listen(
\App\Events\NewArticleFetched::class,
\App\Listeners\ValidateArticleListener::class,
);
Event::listen(
\App\Events\ArticleApproved::class,
\App\Listeners\PublishApprovedArticle::class,
);
Event::listen(
\App\Events\ArticleReadyToPublish::class,
\App\Listeners\PublishArticle::class,
);
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) {
$exception instanceof Error => LogLevelEnum::CRITICAL,
$exception instanceof InvalidArgumentException => LogLevelEnum::WARNING,
default => LogLevelEnum::ERROR,
};
}
}