Needs Exception class for 301 Error
Looks like you forgot to create a 301 Error Exception Class. It's the first one I ran into needing and looks like it doesn't exist.
Is there a way to get the "code" value from the HTTPException class somehow?
They provided redirect function with both javascript & header redirection mechanics. I agree though that 301 Exception is required as well. Sometimes you want to raise redirect from inside deep layered function where multiple return stack would be clumsy.
As for the second question, HttpException has code attribute. How did you miss it?
Why should redirects be errors?
I would probably never raise a redirect error, but I need some way to capture that a redirect was requested. Consider the following:
try: load_page() except 301Error: pre_pare_for_redirect() // maybe send an email, or run some other code redirect(): except 404Error: go_to_404_page()
@untitaker Exception is NOT an error. Exception is a way to escape from deep-layered code. The reasons to escape can be different. Did you never see BreakException or something like this?
The OP implied in the title that 301 is an error. I am well-aware what exceptions are, thanks.
Yes, the title is kinda broken. Probably he should asked about Http301 or RedirectException instead. What does it change?
It seems to be on purpose like that though, as the whole 3** block doesn't have equivalent exceptions.
Yes, the whole block is absent. I see only two possible reasons for this:
- duplicate 301 redirect header with javascript redirect mechanics (old browsers support?)
- mirror
return Response(..)withreturn redirect(..)
I just vote +1 for adding those Exceptions.
+1 from me as well. I've already reported this same issue for Flask. Should have looked deeper, I guess.
I raised it as I was writing an API that is consumed as JSON and needed the HTTP errors to return as JSON as well. Was easy enough to do as Flask allows you to override the HTTP exception handler. Problem is that 300 errors aren't exceptions.
You can always abort(redirect('...')).