werkzeug icon indicating copy to clipboard operation
werkzeug copied to clipboard

Needs Exception class for 301 Error

Open EvHaus opened this issue 13 years ago • 10 comments

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?

EvHaus avatar Apr 17 '13 22:04 EvHaus

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?

ivan-kleshnin avatar Apr 25 '13 11:04 ivan-kleshnin

Why should redirects be errors?

untitaker avatar Apr 25 '13 12:04 untitaker

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()

EvHaus avatar Apr 25 '13 14:04 EvHaus

@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?

ivan-kleshnin avatar Apr 26 '13 07:04 ivan-kleshnin

The OP implied in the title that 301 is an error. I am well-aware what exceptions are, thanks.

untitaker avatar Apr 26 '13 07:04 untitaker

Yes, the title is kinda broken. Probably he should asked about Http301 or RedirectException instead. What does it change?

ivan-kleshnin avatar Apr 26 '13 07:04 ivan-kleshnin

It seems to be on purpose like that though, as the whole 3** block doesn't have equivalent exceptions.

untitaker avatar Apr 26 '13 10:04 untitaker

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(..) with return redirect(..)

I just vote +1 for adding those Exceptions.

ivan-kleshnin avatar Apr 26 '13 12:04 ivan-kleshnin

+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.

sergedomk avatar Apr 26 '13 14:04 sergedomk

You can always abort(redirect('...')).

mitsuhiko avatar May 31 '13 15:05 mitsuhiko