Skip to content

Commit

Permalink
flask_util: add werkzeug exceptions for 301/302 redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Jun 7, 2023
1 parent 6e55c8f commit 4d5e551
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions flask_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ class NoContent(HTTPException):
code = 204
description = 'No Content'

class Redirect(HTTPException):
def __init__(self, *args, location=None, **kwargs):
assert location
self.location = location
super().__init__(**kwargs)

def get_headers(self, *args, **kwargs):
return {'Location': self.location}

class MovedPermanently(Redirect):
code = 301
description = 'Moved Permanently'

class Found(Redirect):
code = 302
description = 'Found'

class NotModified(HTTPException):
code = 304
description = 'Not Modified'
Expand Down Expand Up @@ -101,11 +118,26 @@ class NetworkConnectTimeoutError(HTTPException):
description = 'Network Connect Timeout Error'


for cls in (Created, Accepted, NoContent, NotModified, PaymentRequired,
ProxyAuthenticationRequired, MisdirectedRequest, UpgradeRequired,
PreconditionRequired, ClientClosedRequest, VariantAlsoNegotiates,
InsufficientStorage, LoopDetected, NotExtended,
NetworkAuthenticationRequired, NetworkConnectTimeoutError):
for cls in (
Created,
Accepted,
NoContent,
MovedPermanently,
Found,
NotModified,
PaymentRequired,
ProxyAuthenticationRequired,
MisdirectedRequest,
UpgradeRequired,
PreconditionRequired,
ClientClosedRequest,
VariantAlsoNegotiates,
InsufficientStorage,
LoopDetected,
NotExtended,
NetworkAuthenticationRequired,
NetworkConnectTimeoutError,
):
# https://github.com/pallets/flask/issues/1837#issuecomment-304996942
werkzeug.exceptions.default_exceptions.setdefault(cls.code, cls)
werkzeug.exceptions._aborter.mapping.setdefault(cls.code, cls)
Expand Down

0 comments on commit 4d5e551

Please sign in to comment.