• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Creating Custom Error

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 24314
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your time. I got the solution.
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic