diff --git a/app/Domain/Catalog/ShinkanImport.php b/app/Domain/Catalog/ShinkanImport.php index 9a10cc7..94669d3 100644 --- a/app/Domain/Catalog/ShinkanImport.php +++ b/app/Domain/Catalog/ShinkanImport.php @@ -629,6 +629,34 @@ private function plain(Throwable $exception): string }; } - return $exception instanceof DomainRuleException ? $exception->getMessage() : 'Die Quelle ist nicht erreichbar.'; + if ($exception instanceof DomainRuleException) { + return $exception->getMessage(); + } + + return $this->networkCause($exception); + } + + /** + * Benennt die Ursache eines Netzwerkfehlers, ohne Adresse, Pfad oder Schlüssel auszugeben. Genannt wird nur der + * Servername, damit die Redaktion erkennt, ob die richtige Quelle gemeint ist. + */ + private function networkCause(Throwable $exception): string + { + $host = app(ShinkanConnection::class)->url(); + $host = $host === null ? 'der Quelle' : (string) (parse_url($host, PHP_URL_HOST) ?: 'der Quelle'); + preg_match('/cURL error (\d+)/', $exception->getMessage(), $match); + $code = isset($match[1]) ? (int) $match[1] : null; + + $cause = match (true) { + $code === 6 => 'Der Servername konnte von diesem Server aus nicht aufgelöst werden (Namensauflösung, DNS).', + $code === 7 => 'Der Server nimmt die Verbindung nicht an (abgelehnt, falscher Port oder Firewall).', + $code === 28 => 'Die Verbindung hat die Zeit überschritten (ausgehende Verbindungen sind möglicherweise blockiert).', + in_array($code, [35, 51, 58, 60, 77, 83], true) => 'Das Zertifikat oder die verschlüsselte Verbindung wird nicht akzeptiert.', + $code === 56 || $code === 52 => 'Die Verbindung wurde unterbrochen.', + $code !== null => 'Netzwerkfehler (cURL '.$code.').', + default => 'Netzwerkfehler.', + }; + + return 'Die Quelle '.$host.' ist von diesem Server aus nicht erreichbar. '.$cause; } } diff --git a/tests/Feature/ShinkanUebernahmeTest.php b/tests/Feature/ShinkanUebernahmeTest.php index 87d0383..7283d90 100644 --- a/tests/Feature/ShinkanUebernahmeTest.php +++ b/tests/Feature/ShinkanUebernahmeTest.php @@ -18,6 +18,7 @@ use App\Models\User; use Filament\Facades\Filament; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Client\Factory; use Illuminate\Support\Facades\Http; use Livewire\Livewire; @@ -465,6 +466,31 @@ public function test_the_address_may_name_the_installation_or_its_api_path_witho } } + public function test_a_network_failure_names_the_cause_and_the_host_but_no_address_path_or_key(): void + { + config(['services.shinkan.url' => 'https://dev.shinkan.jinkendo.de']); + $cases = [ + 'cURL error 6: Could not resolve host: dev.shinkan.jinkendo.de (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://dev.shinkan.jinkendo.de/api/distribution/v1/catalog' => 'Namensauflösung', + 'cURL error 7: Failed to connect to dev.shinkan.jinkendo.de port 443' => 'Firewall', + 'cURL error 28: Connection timed out after 20001 milliseconds' => 'Zeit überschritten', + 'cURL error 60: SSL certificate problem' => 'Zertifikat', + 'irgendetwas anderes' => 'Netzwerkfehler.', + ]; + + foreach ($cases as $message => $expected) { + $this->freshHttp(); + Http::fake(fn () => throw new ConnectionException($message)); + + $result = app(ShinkanImport::class)->check(); + + $this->assertFalse($result['ok']); + $this->assertStringContainsString('dev.shinkan.jinkendo.de', $result['message']); + $this->assertStringContainsString($expected, $result['message']); + $this->assertStringNotContainsString('/api/', $result['message']); + $this->assertStringNotContainsString('geheim-fuer-den-test', $result['message']); + } + } + public function test_the_exercise_list_leads_to_the_takeover_page(): void { Livewire::actingAs($this->staff('redakteur'), 'staff')