changeset 30:9ba4f8cc32f1

Display the returned error in the title of the spinner.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 23 Dec 2018 17:15:38 +0100
parents 15666446c791
children e561bdd81777
files util.js vcard.js
diffstat 2 files changed, 24 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/util.js
+++ b/util.js
@@ -53,10 +53,25 @@ function configurePEPField(node, key, va
                         .up();
 }
 
+function parseErrorIq(iq) {
+    // TODO: actually check that it is the first one.
+    const error = iq.firstChild;
+    if (error.namespaceURI !== 'jabber:client' || error.localName !== 'error')
+        return null;
+    const condition = error.firstChild;
+    if (condition.namespaceURI !== 'urn:ietf:params:xml:ns:xmpp-stanzas')
+        return null;
+    const text = error.lastChild;
+    if (text.namespaceURI !== 'urn:ietf:params:xml:ns:xmpp-stanzas' || text.localName !== 'text')
+        return null;
+    return [condition.localName, text.textContent];
+}
+
 function displaySpinner(spinner) {
     if ('timeoutid' in spinner.dataset)
         clearTimeout(spinner.dataset.timeoutid);
     spinner.src = 'spinner.svg';
+    spinner.title = '';
     spinner.hidden = false;
 }
 
@@ -64,16 +79,18 @@ function spinnerOk(spinner) {
     if ('timeoutid' in spinner.dataset)
         clearTimeout(spinner.dataset.timeoutid);
     spinner.src = 'ok.svg';
+    spinner.title = '';
     spinner.hidden = false;
     spinner.dataset.timeoutid = setTimeout(function () {
         spinner.hidden = true;
     }, 1000);
 }
 
-function spinnerError(spinner) {
+function spinnerError(spinner, title) {
     if ('timeoutid' in spinner.dataset)
         clearTimeout(spinner.dataset.timeoutid);
     spinner.src = 'error.svg';
+    spinner.title = title ? title : '';
     spinner.hidden = false;
 }
 
--- a/vcard.js
+++ b/vcard.js
@@ -61,7 +61,7 @@ function initVCard(connection) {
         if (vcard_bday.value)
             iq.c('bday')
                 .c('date').t(vcard_bday.value).up().up();
-        connection.sendIQ(iq, onVCard4Changed, onVCard4ChangeError.bind(null, 'coucou'));
+        connection.sendIQ(iq, onVCard4Changed, onVCard4ChangeError);
         displaySpinner(spinner_img);
     }
 
@@ -75,10 +75,12 @@ function initVCard(connection) {
         spinnerOk(spinner_img);
     }
 
-    function onVCard4ChangeError(string)
+    function onVCard4ChangeError(iq)
     {
-        console.log('Failed to set vCard4: ' + string);
-        spinnerError(spinner_img);
+        const [condition, text] = parseErrorIq(iq);
+        const string = 'Failed to set vCard4: ' + condition + ': ' + text;
+        console.log(string);
+        spinnerError(spinner_img, string);
     }
 
     vcard_access.addEventListener('change', function (evt) {