commit dde3db306cb128e28d2bf8db7ea37df9b85f6176
Author: Jan Pašek <janpasek97@gmail.com>
Date:   Wed May 12 09:52:23 2021 +0200

    Re #8587 - Error handling improved in all javascript files

diff --git a/static/js/certificate.js b/static/js/certificate.js
index 351e835..a072c87 100644
--- a/static/js/certificate.js
+++ b/static/js/certificate.js
@@ -89,40 +89,69 @@ var certificateDetailsApp = new Vue({
                         this.issuerLoaded = true;
                     } else {
                         // certificate does not exists
-                        console.error("Required certificate does not exists");
-                        this.showError("Error occurred while loading issuer's details.");
+                        console.error(response.data["data"]);
+                        this.errorMessage = "Error occurred while loading issuer's details.";
                     }
                 } catch (error) {
                     console.error(error);
-                    this.showError("Error occurred while loading issuer's details.");
+                    this.errorMessage = "Error occurred while loading issuer's details.";
                 }
             }
             this.displayIssuer = ~this.displayIssuer;
         },
         onCertificateDownload: async function () {
-            await onCertificateDownload(this.id);
+            try {
+                await onCertificateDownload(this.id);
+            }
+            catch (err) {
+                this.errorMessage = err.message;
+            }
         },
         onRootDownload: async function () {
-            await onCertificateRootDownload(this.id);
+            try {
+                await onCertificateRootDownload(this.id);
+            }
+            catch (err) {
+                this.errorMessage = err.message;
+            }
         },
         onChainDownload: async function () {
-            await onCertificateChainDownload(this.id);
+            try {
+                await onCertificateChainDownload(this.id);
+            }
+            catch (err) {
+                this.errorMessage = err.message;
+            }
         },
         onPublicKeyDownload: async function () {
-            await onPublicKeyDownload(this.id);
+            try {
+                await onPublicKeyDownload(this.id);
+            }
+            catch (err) {
+                this.errorMessage = err.message;
+            }
         },
         onPrivateKeyDownload: async function () {
-            await onPrivateKeyDownload(this.id);
+            try {
+                await onPrivateKeyDownload(this.id);
+            }
+            catch (err) {
+                this.errorMessage = err.message;
+            }
         },
         onIdentityDownload: async function() {
-            this.errorMessage = "";
-            if (this.identityName === "") {
-                this.errorMessage = "Identity name must not be empty! Please try again.";
-                document.body.scrollTop = 0;
-                document.documentElement.scrollTop = 0;
-                return;
-            }
-            await onIdentityDownload(this.id, this.identityName, this.identityPass);
+            try {
+                this.errorMessage = "";
+                if (this.identityName === "") {
+                    this.errorMessage = "Identity name must not be empty! Please try again.";
+                    document.body.scrollTop = 0;
+                    document.documentElement.scrollTop = 0;
+                    return;
+                }
+                await onIdentityDownload(this.id, this.identityName, this.identityPass);
+            } catch (err) {
+                this.errorMessage = err.message;
+            }
         },
         onRevoke: async function () {
             document.body.scrollTop = 0;
@@ -136,9 +165,11 @@ var certificateDetailsApp = new Vue({
                     this.certificate.status="revoked";
                     certificateDetailsApp.successMessage = "Certificate revocation successful.";
                 } else {
+                    console.error(response.data["data"]);
                     certificateDetailsApp.errorMessage = "Certificate cannot be revoked - " + response.data["data"] + "!";
                 }
             } catch (error) {
+                console.error(error);
                 certificateDetailsApp.errorMessage = "An error occurred while revoking certificate";
             }
         },
@@ -150,9 +181,11 @@ var certificateDetailsApp = new Vue({
                 if (response.data["success"]) {
                     window.location.href = "/static/index.html?success=Certificate+successfully+deleted";
                 } else {
+                    console.error(response.data["data"]);
                     certificateDetailsApp.errorMessage = "Certificate cannot be deleted - " + response.data["data"] + "!";
                 }
             } catch (error) {
+                console.error(error);
                 certificateDetailsApp.errorMessage = "An error occurred while deleting certificate";
             }
         }
@@ -177,6 +210,7 @@ var certificateDetailsApp = new Vue({
                     } else {
                         // certificate does not exists
                         console.error("Required certificate does not exists");
+                        console.error(response.data["data"])
                         certificateDetailsApp.loading = false;
                         certificateDetailsApp.error = true;
                     }
@@ -214,7 +248,7 @@ var certificateDetailsApp = new Vue({
                 certificateDetailsApp.issuedCertificates = [];
             }
         } catch (error) {
-            console.log(error);
+            console.error(error);
         }
         this.loading = false;
         this.error = false;
diff --git a/static/js/create_certificate.js b/static/js/create_certificate.js
index e55fa86..10bd937 100644
--- a/static/js/create_certificate.js
+++ b/static/js/create_certificate.js
@@ -66,9 +66,12 @@ var createCertificateApp = new Vue({
             if (response.data["success"]) {
                 createCertificateApp.authorities = response.data["data"];
             } else {
-                createCertificateApp.authorities = []
+                this.showError("Error occurred while downloading list of available CAs");
+                console.error(response.data["data"]);
+                createCertificateApp.authorities = [];
             }
         } catch (error) {
+            this.showError("Error occurred while downloading list of available CAs");
             console.log(error);
         }
     },
@@ -93,7 +96,9 @@ var createCertificateApp = new Vue({
         // handle certificate creation request
         onCreateCertificate: async function () {
             // validate input data
+            // - validate if is self signed or CA is selected
             // - validate if subject CN is filled in
+            // - validate if C is either empty or has exactly 2 characters
             if (!this.isSelfSigned && this.certificateData.CA == null) {
                 this.showError("Issuer must be selected or 'Self-signed' option must be checked!")
                 return;
@@ -142,10 +147,11 @@ var createCertificateApp = new Vue({
                 }
                 // on error display server response message
                 else {
+                    console.error(response.data["data"]);
                     createCertificateApp.showError(response.data["data"]);
                 }
             } catch (error) {
-                createCertificateApp.showError(error.response.data["data"]);
+                createCertificateApp.showError("An error occurred while creating a certificate.");
                 console.error(error);
             }
         }
diff --git a/static/js/index.js b/static/js/index.js
index 48ba6b9..92c363e 100644
--- a/static/js/index.js
+++ b/static/js/index.js
@@ -43,6 +43,7 @@ var certificateListingApp = new Vue({
         },
         loadPage: async function (page, perPage) {
             // download a list of all available certificates and display them in the table
+            this.errorMessage = "";
             try {
                 var params = {
                     page: page - 1,
@@ -59,7 +60,12 @@ var certificateListingApp = new Vue({
                     this.hasPreviousPage = page > 1;
                     this.hasNextPage = await this.checkIfNextPage(page, this.certificatesPerPage);
                 }
+                else {
+                    this.errorMessage = "Error occurred while loading certificate page.";
+                    console.error(response.data["data"]);
+                }
             } catch (error) {
+                this.errorMessage = "Error occurred while loading certificate page.";
                 console.log(error);
             }
         },
@@ -119,8 +125,11 @@ var certificateListingApp = new Vue({
                 if (response.data["success"]) {
                     return response.data["data"].length > 0;
                 }
+                this.errorMessage = "Error occurred while loading certificate page.";
                 return false;
             } catch (error) {
+                this.errorMessage = "Error occurred while loading certificate page.";
+                console.error(error);
                 return false;
             }
         }
diff --git a/static/js/utilities.js b/static/js/utilities.js
index 224ac8c..5af07f7 100644
--- a/static/js/utilities.js
+++ b/static/js/utilities.js
@@ -42,15 +42,20 @@ Date.prototype.toDateInputValue = (function () {
  * @param id identifier of certificate to be downloaded
  */
 async function onCertificateDownload(id) {
+    var success = false;
     try {
         const response = await axios.get(API_URL + "certificates/" + id);
         if (response.data["success"]) {
+            success = true;
             download(id + ".pem", response.data["data"])
-        } else
-            console.log("Error occurred while downloading the certificate") // TODO more action may be required
+        } else {
+            console.error("Error occurred while downloading the certificate");
+            console.error(response.data["data"]);
+        }
     } catch (error) {
         console.log(error);
     }
+    if (!success) throw new Error("Error occurred while obtaining the requested data!");
 }
 
 /**
@@ -58,15 +63,20 @@ async function onCertificateDownload(id) {
  * @param id identifier of certificate whose chain shall be downloaded
  */
 async function onCertificateChainDownload(id) {
+    var success = false;
     try {
         const response = await axios.get(API_URL + "certificates/" + id + "/chain");
         if (response.data["success"]) {
+            success = true;
             download(id + "_chain.pem", response.data["data"])
-        } else
-            console.log("Error occurred while downloading the certificate's chain of trust") // TODO more action may be required
+        } else {
+            console.error("Error occurred while downloading the certificate's chain of trust");
+            console.error(response.data["data"]);
+        }
     } catch (error) {
         console.log(error);
     }
+    if (!success) throw new Error("Error occurred while obtaining the requested data!");
 }
 
 /**
@@ -74,15 +84,20 @@ async function onCertificateChainDownload(id) {
  * @param id identifier of certificate whose root shall be downloaded
  */
 async function onCertificateRootDownload(id) {
+    var success = false;
     try {
         const response = await axios.get(API_URL + "certificates/" + id + "/root");
         if (response.data["success"]) {
+            success = true;
             download(id + "_root.pem", response.data["data"])
-        } else
-            console.log("Error occurred while downloading the certificate's root CA") // TODO more action may be required
+        } else {
+            console.error("Error occurred while downloading the certificate's root CA");
+            console.error(response.data["data"]);
+        }
     } catch (error) {
         console.log(error);
     }
+    if (!success) throw new Error("Error occurred while obtaining the requested data!");
 }
 
 /**
@@ -90,15 +105,20 @@ async function onCertificateRootDownload(id) {
  * @param id identifier of certificate whose pub. key shall be downloaded
  */
 async function onPublicKeyDownload(id) {
+    var success = false;
     try {
         const response = await axios.get(API_URL + "certificates/" + id + "/publickey");
         if (response.data["success"]) {
+            success = true;
             download(id + "_public_key.pem", response.data["data"])
-        } else
-            console.log("Error occurred while downloading the certificate's public key") // TODO more action may be required
+        } else {
+            console.error("Error occurred while downloading the certificate's public key")
+            console.error(response.data["data"]);
+        }
     } catch (error) {
         console.log(error);
     }
+    if (!success) throw new Error("Error occurred while obtaining the requested data!");
 }
 
 /**
@@ -106,15 +126,20 @@ async function onPublicKeyDownload(id) {
  * @param id identifier of certificate whose pub. key shall be downloaded
  */
 async function onPrivateKeyDownload(id) {
+    var success = false;
     try {
         const response = await axios.get(API_URL + "certificates/" + id + "/privatekey");
         if (response.data["success"]) {
+            success = true;
             download(id + "_private_key.pem", response.data["data"])
-        } else
-            console.log("Error occurred while downloading the certificate's private key") // TODO more action may be required
+        } else {
+            console.error("Error occurred while downloading the certificate's private key");
+            console.error(response.data["data"]);
+        }
     } catch (error) {
         console.log(error);
     }
+    if (!success) throw new Error("Error occurred while obtaining the requested data!");
 }
 
 /**
@@ -124,6 +149,7 @@ async function onPrivateKeyDownload(id) {
  * @param passphrase password protecting the identity
  */
 async function onIdentityDownload(id, name, passphrase) {
+    var success = false;
     try {
         var identityData = {
             name: name,
@@ -136,8 +162,10 @@ async function onIdentityDownload(id, name, passphrase) {
                 'Accept': 'application/x-pkcs12 '
             }
         });
+        success = true;
         downloadBinary(name + ".p12", response.data)
     } catch (error) {
         console.log(error);
     }
+    if (!success) throw new Error("Error occurred while obtaining the requested data!");
 }
\ No newline at end of file
