From ea37e62fa2e98fa0e95f88eaa114366828d209b9 Mon Sep 17 00:00:00 2001 From: Lukas Schneider <lukas.s.schneider@fau.de> Date: Fri, 20 Sep 2019 11:51:21 +0200 Subject: [PATCH] Remove duplicate error handling code --- store/templates/base.html | 10 +++++----- store/templates/buy.html | 12 ++---------- store/templates/charge.html | 12 ++---------- store/templates/transfer.html | 12 ++---------- 4 files changed, 11 insertions(+), 35 deletions(-) diff --git a/store/templates/base.html b/store/templates/base.html index bc3ef39..edbc7f7 100644 --- a/store/templates/base.html +++ b/store/templates/base.html @@ -156,11 +156,11 @@ {% comment %} Error messages {% endcomment %} <script> // Shows an error in the alert div. - // If there is an error message in the response, use it. - // Otherwise use the def. - function showError(response, def) { - if (response && "error" in response) { - displayError(response.error); + // Try to extract an error message from error. + // Otherwise use def. + function showError(error, def) { + if (error && "responseJSON" in error && "error" in error.responseJSON) { + displayError(error.responseJSON.error); } else { displayError(def) } diff --git a/store/templates/buy.html b/store/templates/buy.html index c56ca61..9999c12 100644 --- a/store/templates/buy.html +++ b/store/templates/buy.html @@ -127,11 +127,7 @@ function buy(id) { error = error => { - err = null; - if (error && "responseJSON" in error) { - err = error.responseJSON - } - showError(err, "Fehler beim Einkaufen"); + showError(error, "Fehler beim Einkaufen"); } // Check if id is a valid integer @@ -176,11 +172,7 @@ function revert(id) { error = error => { - err = null; - if (error && "responseJSON" in error) { - err = error.responseJSON - } - showError(err, "Fehler beim Rückgägig machen eines Einkaufes."); + showError(error, "Fehler beim Rückgägig machen eines Einkaufes."); } return function() { diff --git a/store/templates/charge.html b/store/templates/charge.html index 578d043..0a0afd1 100644 --- a/store/templates/charge.html +++ b/store/templates/charge.html @@ -111,11 +111,7 @@ function charge() { error = error => { - err = null; - if (error && "responseJSON" in error) { - err = error.responseJSON; - } - showError(err, "Fehler beim Aufladen"); + showError(error, "Fehler beim Aufladen"); } // Get amount from text field @@ -161,11 +157,7 @@ // Returns a function that can be called to revert the charge where charge.id == id function revert(id) { error = error => { - err = null; - if (error && "responseJSON" in error) { - err = error.responseJSON - } - showError(err, "Fehler beim Rückgägig machen einer Aufladung"); + showError(error, "Fehler beim Rückgägig machen einer Aufladung"); }; return function() { diff --git a/store/templates/transfer.html b/store/templates/transfer.html index 0f50876..7cdebe4 100644 --- a/store/templates/transfer.html +++ b/store/templates/transfer.html @@ -175,11 +175,7 @@ function transfer(receiver) { error = error => { - err = null; - if (error && "responseJSON" in error) { - err = error.responseJSON; - } - showError(err, "Fehler beim Überweisen"); + showError(error, "Fehler beim Überweisen"); } @@ -242,11 +238,7 @@ // Returns a function that can be called to revert the transfer where transfer.id == id function revert(id) { error = error => { - err = null; - if (error && "responseJSON" in error) { - err = error.responseJSON; - } - showError(err, "Fehler beim Rückgägig machen einerÜberweisung"); + showError(error, "Fehler beim Rückgägig machen einerÜberweisung"); } return function() { -- GitLab