signalpoint / DrupalGap

An application development kit for Drupal websites.
https://www.drupalgap.org
GNU General Public License v2.0
234 stars 186 forks source link

How to check if Drupal.settings.site_path can be resolved? #909

Open luxio opened 7 years ago

luxio commented 7 years ago

In my app, Drupal.settings.site_path can be configured by the user.

Is there a way, to check, if the Drupal.settings.site_path can be resolved? If the server cannot be resolved I want to go back to login form (where the user can edit the Drupal.settings.site_path and try again).

If the user enters a non existing host, DG freezes. On iOS I don't get an error in the console.

Using ripple, I get this error if the the host cannot be resolved:

ripple.js:50 GET https://nonexisiting.server.com/?q=services/session/token net::ERR_NAME_NOT_RESOLVED

Or if the server is in maintenance mode:

POST: https://example.com/?q=app/system/connect.json
jdrupal-7.0.5.min.js:2 Drupal.services.call - services_get_csrf_token - success - SyntaxError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': '<html>
signalpoint commented 7 years ago

@luxio The latest dev snapshot of jdrupal.min.js (I really need to release another recommended release soon) has some big time improvements to error handling when using jDrupal's Service Resource layer.

When attaching a typical error handler to a service resource call in DrupalGap, that error handler is now being hit when some odd networking errors occur. I don't recall is I've come across the ERR_NAME_NOT_RESOLVED response before, but you can see here in jDrupal where I was able to add coverage for strange networking issues:

https://github.com/signalpoint/jDrupal/blob/7.x-1.x/src/services/services.js#L35

So as long as your call to services/session/token is running through the jDrupal Services layer, you should be able to catch that error, for example:

services_get_csrf_token({
  success: function(token) { alert('Got the token' + token); },
  error: function(xhr, status, msg) {
    // Something went wrong...
    console.log(arguments);
    alert(msg);
  }
});

The status and xhr values should both contain good debugging info that you can then relay to the user in the error handler.

luxio commented 7 years ago

@signalpoint Thank you for the info. I will definitely check it out.

Meanwhile I have resolved this by checking for a file on the server:

function mymodule_user_login_submit_check_site_path(form, form_state) {
  try {
    // check if server can be reached
    var xhr = new XMLHttpRequest();
    var submitted_sitepath = 'https://' + form_state.values.mymodule_site_path;
    var file = submitted_sitepath+'/files/test.gif';
    var randomNum = Math.round(Math.random() * 10000);

    xhr.open('HEAD', file + "?rand=" + randomNum, true);
    xhr.send();

    xhr.addEventListener("readystatechange", processRequest, false);

    function processRequest(e) {
      if (xhr.readyState == 4) {
        if (xhr.status >= 200 && xhr.status < 304) {
          // can connect to server
          mymodule_user_login_submit(form, form_state);
        } else {
          var msg = t('Failed to connect to') + ' ' + submitted_sitepath  + ' ' +
            t('Please check the site name.');
          drupalgap_alert(msg, {
            title: t('Connection Failure'),
            buttonName: t('OK'),
          });
        }
      }
    }
  } catch (error) {
    console.log('mymodule_user_login_submit_check_site_path  - ' + error);
  }
}