From 26e2f4b9a00927b1515367e4e64d4bcb181965a8 Mon Sep 17 00:00:00 2001
From: Julian Keck <julian.keck9@kit.edu>
Date: Mon, 11 Nov 2024 16:37:27 +0100
Subject: [PATCH] FIX: better error-handling for ip-contact

Closes #776
---
 src/locales/de.json           |  2 ++
 src/locales/en.json           |  2 ++
 src/views/tools/IPContact.vue | 22 ++++++++++++++++++----
 3 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/locales/de.json b/src/locales/de.json
index 93e8b80ab..11ebcd73c 100644
--- a/src/locales/de.json
+++ b/src/locales/de.json
@@ -504,6 +504,7 @@
     "impersonating": "Betrachtet als",
     "incompatible_token": "Ihr Sitzungs-Token ist inkompatibel mit der aktuellen Version des NETVS. Bitte melden Sie sich ab und wieder an und löschen Sie ggf. alte Sitzungs-Tokens manuell aus Ihrem Account.",
     "incompatible_token_title": "Inkompatibles Sitzungs-Token",
+    "internal_error": "Internet (Netzwerk-)Fehler",
     "invalid_impersonate_user_event": "Der angegebene Account als den Sie sich ausgeben wollten existiert nicht. Sie agieren nun wieder im Kontext Ihres eigenen Accounts.",
     "invalid_impersonate_user_event_title": "Ungültiger Benutzername",
     "invalid_ipv6": "Ungültige IPv6-Adresse!",
@@ -966,6 +967,7 @@
       "ip_contact": {
         "description": "Dieses Formular erlaubt es Ihnen, die Adressbetreuer von einer angegebenen Adresse zu kontaktieren.",
         "failed": "Senden der Anfrage fehlgeschlagen! Fehlerursache: ",
+        "invalid_ip": "Ungültige IP-Adresse",
         "message": "Nachricht",
         "submit": "Absenden",
         "success": "E-Mail erfolgreich an die Adressbetreuer versendet!"
diff --git a/src/locales/en.json b/src/locales/en.json
index 181cea7d2..810778e36 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -501,6 +501,7 @@
     "impersonating": "Impersonating",
     "incompatible_token": "You session token is incompatible with the current version of NETVS. Please log out and log in again and delete old session tokens from your account.",
     "incompatible_token_title": "Incompatible session token",
+    "internal_error": "Internal (network-)error",
     "invalid_impersonate_user_event": "The account you tried to impersonate does not exist. Falling back to your own account.",
     "invalid_impersonate_user_event_title": "Invalid username",
     "invalid_ipv6": "Invalid IPv6 Address!",
@@ -974,6 +975,7 @@
       "ip_contact": {
         "description": "This form allows you to contact the network administrators of a given IP-address.",
         "failed": "Message could not be sent! Reason: ",
+        "invalid_ip": "Invalid ip-address",
         "message": "Message",
         "submit": "Send",
         "success": "E-Mail to BCD-administrators was sent successfully!"
diff --git a/src/views/tools/IPContact.vue b/src/views/tools/IPContact.vue
index 73d9698d6..045c159b0 100644
--- a/src/views/tools/IPContact.vue
+++ b/src/views/tools/IPContact.vue
@@ -17,10 +17,14 @@
       variant="danger">
       {{ $t('views.tools.ip_contact.failed') }}{{ failure_reason }}
     </b-alert>
-
+    <b-alert
+      :show="check_cidr === false && show_ip_error"
+      variant="danger">
+      {{ $t('views.tools.ip_contact.invalid_ip') }}
+    </b-alert>
     <b-form @submit="submitMessage">
       <b-form-group :label="$t('system.ip_address_or_cidr') + ':'">
-        <b-input required v-model="cidr" :placeholder="$t('system.ip_address_or_cidr')" :state="check_cidr"></b-input>
+        <b-input required v-model="cidr" :placeholder="$t('system.ip_address_or_cidr')" :state="check_cidr" @change="() => {if (cidr.length === 0) {show_ip_error = false}}"></b-input>
       </b-form-group>
       <b-textarea v-model="message" :placeholder="$t('views.tools.ip_contact.message')">
 
@@ -47,12 +51,20 @@ export default {
       message: '',
       message_pending: false,
       message_success: undefined,
-      failure_reason: ''
+      failure_reason: '',
+      show_ip_error: false,
     }
   },
   methods: {
     async submitMessage(ev) {
       ev.preventDefault()
+
+      if (!this.check_cidr) {
+        this.show_ip_error = true
+        return false
+      }
+      this.show_ip_error = false
+
       this.message_success = undefined
       this.failure_reason = ''
 
@@ -63,7 +75,9 @@ export default {
         this.message = ''
         setTimeout(() => { this.message_success = undefined }, 10_000)
       }).catch(e => {
-        if (e.response.status === 404) {
+        if (!e.response) {
+          this.failure_reason = this.$t('system.internal_error')
+        } else if (e.response.status === 404) {
           this.failure_reason = e.response.data.detail[this.$store.state.locale] || e.response.detail.en
           this.message_success = false
         } else {
-- 
GitLab