;(function () {
  /* global fetch: false */
  'use strict';

  function getRestaurants(coords) {
    var fetchUrl = '/restaurants?latitude=' + coords.latitude + '&longitude=' + coords.longitude;
    return fetch(fetchUrl)
      .then(function(response) {
        return response.json();
      }).then(function(response) {
        if (response.statusCode >= 200 && response.statusCode < 300) {
          return response;
        }
        throw new Error(response.description);
      });
  }

  var $status = document.getElementById('status');

  if (!navigator.geolocation) {
    $status.innerHTML = 'geolocation is not supported on this platform';
    return;
  }

  $status.innerHTML = 'retrieving your location...';
  navigator.geolocation.getCurrentPosition(function (position) {
    $status.innerHTML = 'your location is: lat(' + position.coords.latitude + '), lon(' + position.coords.longitude + ')';
    console.log('Geolocation position', position);

    getRestaurants(position.coords)
      .then(function (restaurants) {
        $status.innerHTML = restaurants.map(function (r) {
          return r.name + '<br />';
        }).join('');
      }).catch(function (err) {
        $status.innerHTML = 'Error retrieving restaurants<br />' + err;
      });

  }, function (error) {
    $status.innerHTML = 'location could not be retrieved.';
    console.error('Geolocation error', error);
  });
})();

;(function () {
  /* global fetch: false */
  'use strict';

  function getRestaurants(coords) {
    var fetchUrl = '/restaurants?latitude=' + coords.latitude + '&longitude=' + coords.longitude;
    return fetch(fetchUrl)
      .then(function(response) {
        // handle any errors
        if (!response.ok) {
          return response.json().then(function (errorBody) {
            throw new Error(errorBody.description || 'An error occurred while fetching restaurants');
          });
        }

        // no errors, parse response json
        return response.json();
      });
  }

  var $status = document.getElementById('status');

  if (!navigator.geolocation) {
    $status.innerHTML = 'geolocation is not supported on this platform';
    return;
  }

  $status.innerHTML = 'retrieving your location...';
  navigator.geolocation.getCurrentPosition(function (position) {
    $status.innerHTML = 'your location is: lat(' + position.coords.latitude + '), lon(' + position.coords.longitude + ')';
    console.log('Geolocation position', position);

    getRestaurants(position.coords)
      .then(function (restaurants) {
        $status.innerHTML = restaurants.map(function (r) {
          return r.name + '<br />';
        }).join('');
      }).catch(function (err) {
        $status.innerHTML = 'Error retrieving restaurants<br />' + err;
      });

  }, function (error) {
    $status.innerHTML = 'location could not be retrieved.';
    console.error('Geolocation error', error);
  });
})();

Fix status handling in getRestaurants
Fix status handling in getRestaurants

The changes in #15 were meant to fix a bug where we were looking at
an incorrect field for the status code of the fetch response.
However, these changes were not thoroughly tested and actually didn't
completely work as coded, because when the response is a success
it does not have a "statusCode" property on the body. This patch
checks the actual HTTP response status instead of the response body
message.
