Files
siakad/app/Exceptions/DatabaseUnreachable.php
T

27 lines
872 B
PHP

<?php
namespace App\Exceptions;
use Illuminate\Database\QueryException;
use PDOException;
use Throwable;
/** Pengenal galat koneksi DB (SQLSTATE kelas 08), mis. server DB lama tidak terjangkau dari luar jaringan UNTAN. */
final class DatabaseUnreachable
{
public const string MESSAGE = 'Tidak dapat menghubungkan ke server, silakan ganti menggunakan jaringan UNTAN.';
public static function matches(Throwable $e): bool
{
if (! $e instanceof QueryException && ! $e instanceof PDOException) {
return false;
}
// Saat membuka koneksi, kode PDO berisi kode driver (mis. 7); SQLSTATE hanya ada di awal pesan.
$pdo = $e instanceof QueryException ? ($e->getPrevious() ?? $e) : $e;
return str_starts_with((string) $e->getCode(), '08')
|| str_starts_with($pdo->getMessage(), 'SQLSTATE[08');
}
}