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

44 lines
1.1 KiB
PHP

<?php
namespace App\Providers;
use App\Events\ExceptionOccurred;
use App\Listeners\LogExceptionToDatabase;
use App\LogLevelEnum;
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,
);
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,
};
}
}