complete story 8.10 with qa test

This commit is contained in:
Naser Mansour
2026-01-02 23:14:53 +02:00
parent 78b3a01c4d
commit 97dca05562
13 changed files with 908 additions and 29 deletions
+81
View File
@@ -0,0 +1,81 @@
<?php
namespace App\Services;
use App\Contracts\ShouldNotifyAdmin;
use App\Notifications\SystemErrorNotification;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Notification;
use Throwable;
class AdminNotificationService
{
public function notifyError(Throwable $exception): void
{
if (! config('libra.notifications.system_notifications_enabled')) {
return;
}
$adminEmail = config('libra.notifications.admin_email');
if (empty($adminEmail)) {
return;
}
$errorKey = $this->getErrorKey($exception);
$throttleMinutes = config('libra.notifications.throttle_minutes', 15);
if (Cache::has($errorKey)) {
return;
}
Cache::put($errorKey, true, now()->addMinutes($throttleMinutes));
Notification::route('mail', $adminEmail)
->notifyNow(new SystemErrorNotification($exception));
}
protected function getErrorKey(Throwable $exception): string
{
return 'admin_notified:'.md5(get_class($exception).$exception->getMessage());
}
public function shouldNotifyAdmin(Throwable $exception): bool
{
if ($exception instanceof ShouldNotifyAdmin) {
return true;
}
$excludedTypes = [
\Illuminate\Validation\ValidationException::class,
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class,
\Illuminate\Session\TokenMismatchException::class,
];
foreach ($excludedTypes as $type) {
if ($exception instanceof $type) {
return false;
}
}
if ($exception instanceof \Illuminate\Database\QueryException) {
return $this->isCriticalDatabaseError($exception);
}
if ($exception instanceof \Symfony\Component\Mailer\Exception\TransportExceptionInterface) {
return true;
}
return false;
}
protected function isCriticalDatabaseError(\Illuminate\Database\QueryException $e): bool
{
$criticalCodes = ['2002', '1045', '1049'];
return in_array($e->getCode(), $criticalCodes);
}
}