title: 500 internal server error when flask form contains null values
body: <p>I've got this form I'm posting to an app route called submitRegistration, code below...but rather then it returning back some json about the missing field, it's throwing a 500 internal server error. Can anyone comment why?</p>
  
  <pre><code>@app.route('/submitRegistration',methods=['POST', 'GET'])
  def submitRegistration():
      try:
      _contactName = request.form['contact_name']
      _businessName = request.form['business_name']
      _telephone = request.form['telephone']
      _email = request.form['email']
      _address = request.form['address']
      _city = request.form['city']
      _state = request.form['state']
      _zip = request.form['zip']
      _services = request.form['services']
  
      # validate the received values
      if _contactName and _email and _telephone and _address and _city and _state and _zip and _services:
  
          # All Good, let's call MySQL
          conn = mysql.connect()
          cursor = conn.cursor()
          cursor.callproc('sp_submitRegistration',(_contactName,_businessName,_telephone,_email,_address,_city,_state,_zip,_services))
          data = cursor.fetchall()
  
          # SMS Confirmation
          smsClient = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
          message = smsClient.messages.create(
              body=TWILIO_REG_SMS_MESSAGE,
              to=_telephone,
              from_=TWILIO_FROM_TELEPHONE,
          )
  
          if len(data) is 0:
              conn.commit()
              return json.dumps({'message':'Registration submitted successfully !'})
          else:
              return json.dumps({'error':str(data[0])})
      else:
          return json.dumps({'html':'&lt;span&gt;Enter the required fields&lt;/span&gt;'})
  
  except Exception as e:
      return json.dumps({'error':str(e)})
  finally:
      cursor.close()
      conn.close()
  </code></pre>
  
  <p><strong><em>console output</em></strong></p>
  
  <pre><code>WARNING:tornado.access:404 GET /favicon.ico (2620:10d:c091:200::f:c1ec) 24.98ms
  ERROR:__main__:Exception on /submitRegistration [POST]
  Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
      response = self.full_dispatch_request()
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
      rv = self.handle_user_exception(e)
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
      reraise(exc_type, exc_value, tb)
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
      rv = self.dispatch_request()
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
      return self.view_functions[rule.endpoint](**req.view_args)
    File "app.py", line 113, in submitRegistration
      cursor.close()
  UnboundLocalError: local variable 'cursor' referenced before assignment
  ERROR:tornado.access:500 POST /submitRegistration (&lt;IPV6 ADDRESS&gt;) 17.90ms
  </code></pre>
  
  <p>new app route...</p>
  
  <pre><code># Functions 
  @app.route('/submitRegistration',methods=['POST', 'GET'])
  def submitRegistration():
      try:
      _contactName = request.form['contact_name']
      _businessName = request.form['business_name']
      _telephone = request.form['telephone']
      _email = request.form['email']
      _address = request.form['address']
      _city = request.form['city']
      _state = request.form['state']
      _zip = request.form['zip']
      _services = request.form['services']
  
      # validate the received values
      if _contactName and _email and _telephone and _address and _city and _state and _zip and _services != "":
  
              # All Good, let's call MySQL
              conn = mysql.connect()
              cursor = conn.cursor()
              cursor.callproc('sp_submitRegistration',(_contactName,_businessName,_telephone,_email,_address,_city,_state,_zip,_services)    )
              data = cursor.fetchall()
  
          # SMS Confirmation
          smsClient = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
          message = smsClient.messages.create(
              body=TWILIO_REG_SMS_MESSAGE,  
              to=_telephone,
              from_=TWILIO_FROM_TELEPHONE,
              )       
  
          if len(data) is 0:
                  conn.commit()
                  return json.dumps({'message': 'Registration submitted successfully !'})
                  cursor.close()
                  conn.close()
          else:
                  return json.dumps({'error':str(data[0])})
          else:
              return json.dumps({'html': '&lt;span&gt;Enter the required fields&lt;/span&gt;'})
  
      except Exception as e:
          return json.dumps({'error':str(e)})    
  </code></pre>