Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7914673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:11:57+00:00 2026-06-03T14:11:57+00:00

I am trying to raise a 401/403 status for when a user attempts to

  • 0

I am trying to raise a 401/403 status for when a user attempts to access something they don’t have privileges to. I used the Webapp2 Exceptions example which generates the proper error codes for 404/500 “natural” events. Such as going to http://localhost:8080/nourl generates the proper 404 and messing up code generates a 500. But when I use the method such as below to set the code using self.error(XXX) I see the code in the console but it does not show up in the browser. EG If you leave self.error() empty it generates the proper 500 code. If you use self.error(500) the console outputs:

INFO     2012-05-09 18:25:29,549 dev_appserver.py:2891] "GET / HTTP/1.1" 500 -

But the browser is completely blank. Below is an example app that exhibits this behaviour. Simply change the self.error() line to the desired code and run. The expected result would be that it generates the proper response to the browser based on the code supplied, not just when a “natural” event occurs such as 404.

main.py

import webapp2
import wsgiref.handlers
import logging
from google.appengine.api import users


class HomeHandler(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            self.response.out.write("Hi :1", user.nickname)
        else:
            self.error(401)


app = webapp2.WSGIApplication([
    (r'/', HomeHandler),
], debug=True)


def handle_401(request, response, exception):
    logging.exception(exception)
    response.write("401 Error")
    response.set_status(401)


def handle_403(request, response, exception):
    logging.exception(exception)
    response.write("403 Error")
    response.set_status(403)


def handle_404(request, response, exception):
    logging.exception(exception)
    response.write("404 Error")
    response.set_status(404)


def handle_500(request, response, exception):
    logging.exception(exception)
    response.write("500 Error")
    response.set_status(500)

app.error_handlers[401] = handle_401
app.error_handlers[403] = handle_403
app.error_handlers[404] = handle_404
app.error_handlers[500] = handle_500


#  Run the application
def main():
    app.run()

app.yaml

application: 401test
version: 1
runtime: python27
api_version: 1
threadsafe: yes

libraries:
- name: webapp2
  version: latest

handlers:
- url: /.*
  script: main.app
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:11:59+00:00Added an answer on June 3, 2026 at 2:11 pm

    you are using py27 with threadsafe environment.
    in app.yaml you set script: main.app so the code after app is defined is not executed.

    didn’t test it but this should work:

    # create a bare app 
    bare_app = webapp2.WSGIApplication(debug=True)
    
    #define the error handlers
    def handle_401(request, response, exception):
        logging.exception(exception)
        response.write("401 Error")
        response.set_status(401)
    
    def handle_403(request, response, exception):
        logging.exception(exception)
        response.write("403 Error")
        response.set_status(403)
    
    
    def handle_404(request, response, exception):
        logging.exception(exception)
        response.write("404 Error")
        response.set_status(404)
    
    
    def handle_500(request, response, exception):
        logging.exception(exception)
        response.write("500 Error")
        response.set_status(500)
    
    # add the error handlers
    bare_app.error_handlers[401] = handle_401
    bare_app.error_handlers[403] = handle_403
    bare_app.error_handlers[404] = handle_404
    bare_app.error_handlers[500] = handle_500
    
    
    bare_app.router.add((r'/', HomeHandler))
    
    app = bare_app
    

    edit:
    use self.abort() instead of self.error()
    http://webapp-improved.appspot.com/guide/exceptions.html#abort

    you can see the difference in the source code:
    http://code.google.com/p/webapp-improved/source/browse/webapp2.py#574
    while self.error() sets the status code but clears the response, self.abort() executes the function that takes care of the error handling.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to raise a click event from User control and handle it
Hi I am trying to raise the PropertyChanged event of an EntityObject, I don't
I am trying to raise an exception if a user clicks the delete button
I'm trying to raise validation errors: 1. when the user enter a US MSRP
I'm trying to take a numeric value in a string and raise it by
I'm trying to write a unit test that will raise an event on a
I'm trying to create a stored proc that indicates a pay raise for two
I'm trying to raise an event in a mocked interface. I can get this
I am trying to raise a click event on a submit button in a
in my unit test I deliberately trying to raise an OutOfMemoryError exception. I use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.