Handle AJAX errors properly

Different status codes need different messages. Always show the user something.

jQuery · Handle AJAX errors properlyIntermediate
function showError(xhr) {
  let msg = 'Something went wrong. Please try again.';
  if (xhr.status === 0)   msg = 'No connection. Check your internet.';
  if (xhr.status === 401) msg = 'Your session ended. Please log in again.';
  if (xhr.status === 403) msg = 'You are not allowed to do that.';
  if (xhr.status === 404) msg = 'Not found.';
  if (xhr.status === 422 && xhr.responseJSON) msg = Object.values(xhr.responseJSON.errors || {}).join(' ');
  $('#alert').text(msg).show();
  return msg;
}

$.getJSON('/api/secret').fail(showError);
ajaxerrorstatusfail