function uploadError(message) {
  console.error(message);
}

function checkStatus(response) {
  if (response.status >= 200 && response.status < 300) {
    return response;
  } else {
    const message =
      'Fetch error: ' + response.url + ' ' + response.status + ' (' +
      response.statusText + ')';
    const error = new Error(message);
    error.response = response;
    throw error;
  }
}

export function fetchJSON(url) {
  return fetch(url)
    .then(checkStatus)
    .then(response => response.json())
    .catch(error => {
      let message = 'GET error: ' + url;
      if (error.response) {
        message = error.message;
      }
      uploadError(message);
      throw error;
    });
}

export function postForm(url, data) {
  return fetch(url, {
    method: 'POST',
    body: data,
  }).then(checkStatus)
    .then(response => response.json())
    .catch(error => {
      let message = 'POST error: ' + url;
      if (error.response) {
        message = error.message;
      }
      uploadError(message);
      throw error;
    });
}
