I created a function which checks parameters and decide to run the rest of the code. I looks like:
def assert_something(a):
if a>3:
exit(0)
I try to use this something like:
class SomeHandler(webapp2.RequestHandler):
def get(self,parameter):
#check and if something wrong then quit
assert_something(parameter)
#otherwise do other things
the problem is exit() function always creates a response with 500 code.
How can modify this code so that I can exit from this request with 200 code for instance?
The problem is that since you are using a framework a lot of things happens under the hood, for example after one of your handlers returns the headers and other information is sent to the browser. But when you exit you interrupt the normal flow hence the internal Server Error.
What you could do is to have a decorator that asserts that the parameter is valid. Note that still you should provide the user with something useful in case the parameter is invalid.
The decorator could be something like this:
And then use it as: