lib.itmens/mastodon/decorators.py

28 lines
726 B
Python
Raw Normal View History

2020-05-05 23:50:48 +08:00
import functools
from django.http import HttpResponse
2020-05-05 23:50:48 +08:00
from django.shortcuts import render
from django.utils.translation import gettext_lazy as _
from requests.exceptions import Timeout
def mastodon_request_included(func):
2023-01-11 19:11:31 -05:00
"""Handles timeout exception of requests to mastodon, returns http 500"""
2020-05-05 23:50:48 +08:00
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
2020-10-22 21:45:05 +02:00
except (Timeout, ConnectionError):
2020-05-05 23:50:48 +08:00
return render(
2024-03-24 22:28:22 -04:00
args[0],
"common/error.html",
{"msg": _("Timeout connecting to Fediverse.")},
2020-05-05 23:50:48 +08:00
)
2023-01-11 19:11:31 -05:00
2020-05-05 23:50:48 +08:00
return wrapper
class HttpResponseInternalServerError(HttpResponse):
status_code = 500