• 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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)
 
Bartender
Posts: 20124
103
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.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!