title: Python Flask catch multiple Integrity errors = different error messages if user or email throws integrity error
body: <p>I have a simple registration function, where a new user has to give following informations: </p>
  
  <p><strong>Email (is unique)</strong></p>
  
  <p><strong>Username (is unique, primary key)</strong></p>
  
  <p><strong>Password</strong></p>
  
  <p><strong>Confirm Password</strong></p>
  
  <p>Problem:</p>
  
  <p>If the user fills in all informations and username or email are already in database, I get an integrity error, which is okay. I can try catch it and display an information that one of those is already used.</p>
  
  <p>But it would be much more usefull if I could tell exactly what is already used. For example the email could be okay but if the username is used, it should tell "Choose an other username".</p>
  
  <p>Hope you got the Problem.</p>
  
  <p>Thats the code I use (there is also already a verification functionality in it but it works fine and is not important):</p>
  
  <pre><code>@app.route('/register', methods=['GET', 'POST'])
  def register():
  form = NewUserForm()
  if form.validate_on_submit():
      new_user = User(email=form.email.data, username=form.username.data, password=make_hash(form.password.data))
      try:
          if new_user:
              db_session.add(new_user)
              db_session.commit()
              token = generate_confirmation_token(new_user.email)
  
              confirm_url = url_for('confirm_email', token=token, _external=True)
  
              html = render_template('activate.html', confirm_url=confirm_url)
  
              subject = "Bitte bestätigen Sie Ihre Registrierung"
  
              send_email(new_user.email, subject, html)
  
              login_user(new_user)
              flash('Registrierung erfolgreich! Wir haben Ihnen einen Bestätigungsemail auf die, von Ihnen angegebene Adresse zugeschickt. Bestätigen Sie bitte Ihre Registrierung.')
              return redirect(url_for('register'))
          else:
              flash('Neuer Nutzer konnte nicht angelegt werden!')
      except IntegrityError:
          flash('Nutzer oder Email ist bereits vergeben, anderer Nutzername erforderlich')
  return render_template('register.html', form=form)
  </code></pre>
  
  <p>What I need is a way to specify wether user or email throwed the integrity error and to display the apropriate message.</p>
  
  <p>Thanks</p>