<commit_before>var handleError = function(err, $scope, $analytics) {
  var msg = err.data, email = $scope.user.email;
  if (!msg) {
    $scope.signupError = "Couldn't sign up. Please try again.";
    Rollbar.error("Signup failure", {email: $scope.user.email});
    $analytics.eventTrack('Signup failure', {email: email, cause: 'unknown'});
    return;
  }

  if (msg === 'bad code') {
    $scope.signupError = 'The invitation code you entered is not valid.';
    $analytics.eventTrack('Signup failure', {email: email, cause: 'bad invitation code'});

  } else if (msg.match(/Key.*already exists/)) {
    var match = msg.match(/Key \((.*)\)=\((.*)\) already exists/),
      key = match[1], value = match[2];

    $scope.signupError = format('The %s "%s" is already in use. Try logging in instead?', key, value);
    $analytics.eventTrack('Signup failure', {email: email, cause: 'duplicate ' + key});

  } else {
    $scope.signupError = msg;
    $analytics.eventTrack('Signup failure', {email: email, cause: msg});
  }
};

module.exports = function($scope, $analytics, User, Community, ThirdPartyAuth, Invitation, context, projectInvitation) {
  "ngInject";
  $analytics.eventTrack('Signup start');
  $scope.user = {};

  $scope.invitation = Invitation.storedData() || projectInvitation;

  $scope.submit = function(form) {
    form.submitted = true;
    $scope.signupError = null;
    if (form.$invalid) return;

    var params = _.merge({}, $scope.user, {login: true}, projectInvitation);

    User.signup(params).$promise.then(function(user) {
      $analytics.eventTrack('Signup success', {provider: 'password'});
      if (context === 'modal') {
        $scope.$close({action: 'finish'});
      } else {
        $scope.$state.go('onboarding.start');
      }
    }, function(err) {
      handleError(err, $scope, $analytics);
    });
  };

  $scope.validateCode = _.debounce(function(form) {
    if ($scope.invitation) return;

    $scope.authStarted = true;
    $scope.validationDone = false;
    if (!_.isEmpty(form.code.$error)) return;

    Community.validate({
      column: 'beta_access_code',
      constraint: 'exists',
      value: form.code.$viewValue,
      store_value: true
    }, function(resp) {
      if (resp.exists) {
        $scope.isCodeValid = true;
        $scope.signupError = null;
      } else {
        $scope.isCodeValid = false;
      }
      $scope.validationDone = true;
    });
  }, 250);

  $scope.useThirdPartyAuth = function(service, form) {
    $scope.authStarted = true;
    if (form.code) {
      if (!_.isEmpty(form.code.$error)) return;
      if (!$scope.isCodeValid) {
        handleError({data: 'bad code'}, $scope, $analytics);
        return;
      }
    }
    $scope.serviceUsed = service;
    $scope.authDialog = ThirdPartyAuth.openPopup(service);
  };

  $scope.finishThirdPartyAuth = function(error) {
    $scope.$apply(function() {
      $scope.authDialog.close();
      if (error) {
        handleError({data: error}, $scope, $analytics);
      } else {
        $analytics.eventTrack('Signup success', {provider: $scope.serviceUsed});
        $scope.$state.go('onboarding.start');
      }
    });
  };

  $scope.go = function(state) {
    if (context === 'modal') {
      $scope.$close({action: 'go', state: state});
    } else {
      $scope.$state.go(state);
    }
  };
<commit_msg>better signup error handling<commit_after>var handleError = function(err, $scope, $analytics) {
  var msg = err.data, email = $scope.user.email;
  if (!msg) {
    $scope.signupError = "Couldn't sign up. Please try again.";
    Rollbar.error("Signup failure", {email: $scope.user.email});
    $analytics.eventTrack('Signup failure', {email: email, cause: 'unknown'});
    return;
  }

  if (msg === 'bad code') {
    $scope.signupError = 'The invitation code you entered is not valid.';
    $analytics.eventTrack('Signup failure', {email: email, cause: 'bad invitation code'});

  } else if (msg.match(/Key.*already exists/)) {
    var match = msg.match(/Key \((.*)\)=\((.*)\) already exists/),
      key = match[1], value = match[2];

    $scope.signupError = format('The %s "%s" is already in use. Try logging in instead?', key, value);
    $analytics.eventTrack('Signup failure', {email: email, cause: 'duplicate ' + key});

  } else {
    $scope.signupError = msg;
    $analytics.eventTrack('Signup failure', {email: email, cause: msg});
  }
};

module.exports = function($scope, $analytics, User, Community, ThirdPartyAuth, Invitation, context, projectInvitation) {
  "ngInject";
  $analytics.eventTrack('Signup start');
  $scope.user = {};

  $scope.invitation = Invitation.storedData() || projectInvitation;

  $scope.submit = function(form) {
    form.submitted = true;
    $scope.signupError = null;
    if (form.$invalid) return;

    var params = _.merge({}, $scope.user, {login: true}, projectInvitation);

    User.signup(params).$promise.then(function(user) {
      $analytics.eventTrack('Signup success', {provider: 'password'});
      if (context === 'modal') {
        $scope.$close({action: 'finish'});
      } else {
        $scope.$state.go('onboarding.start');
      }
    }, function(err) {
      handleError(err, $scope, $analytics);
    });
  };

  $scope.validateCode = _.debounce(function(form) {
    if ($scope.invitation) return;

    $scope.authStarted = true;
    $scope.validationDone = false;
    if (!_.isEmpty(form.code.$error)) return;

    Community.validate({
      column: 'beta_access_code',
      constraint: 'exists',
      value: form.code.$viewValue,
      store_value: true
    }, function(resp) {
      if (resp.exists) {
        $scope.isCodeValid = true;
        $scope.signupError = null;
      } else {
        $scope.isCodeValid = false;
      }
      $scope.validationDone = true;
    });
  }, 250);

  $scope.useThirdPartyAuth = function(service, form) {
    $scope.authStarted = true;
    if (form.code) {
      if (!_.isEmpty(form.code.$error) || !$scope.isCodeValid) {
        handleError({data: 'bad code'}, $scope, $analytics);
        return;
      }
    }
    $scope.serviceUsed = service;
    $scope.authDialog = ThirdPartyAuth.openPopup(service);
  };

  $scope.finishThirdPartyAuth = function(error) {
    $scope.$apply(function() {
      $scope.authDialog.close();
      if (error) {
        handleError({data: error}, $scope, $analytics);
      } else {
        $analytics.eventTrack('Signup success', {provider: $scope.serviceUsed});
        $scope.$state.go('onboarding.start');
      }
    });
  };

  $scope.go = function(state) {
    if (context === 'modal') {
      $scope.$close({action: 'go', state: state});
    } else {
      $scope.$state.go(state);
    }
  };

{'fraction_non_alphanumeric': 0.17283765552390945, 'fraction_numerical': 0.00149902563333833, 'mean_word_length': 3.0907418761496013, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '357166', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2130, 'n_words': 451}