Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Creating Custom Error  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to display the result if a user types a name in the url i.e localhost:5000/user/username. i want to display the custom error if username is either empty or any number. For that I have written the code as below using python and flask and I'm getting proper result if username is specified but not getting the custom error while giving number instead getting internal server error. Error handler function is not getting executed. I think it's not getting any exception even if the username is empty. what changes I need to make in order to display custom error page.

from flask import Flask
app = Flask(__name__)

@app.route('/user/<username>')
def hello(username):
   if username == username:
       return 'Hello, {}'.format(username)

@app.errorhandler(NameError)
def name_error():
   return 'enter username and then try'

   
if __name__ == '__main__':
#     app.debug = True
   app.run(host= '0.0.0.0', port = 5000)
 
Saloon Keeper
Posts: 20507
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is Python language code, is it not?

Your route statement isn't checking to see if username is a valid string. So it's probably trying to format None as a String or something like that. And I'm not sure I understand why you're comparing username to username, since that seems like pointless work. But mainly I'd add logic to that function to test that username is indeed a valid String and present the error page if it is not.
 
Megha Raj
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your time. I got the solution.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!