Coverage for app/venv/lib/python3.14/site-packages/weblate/trans/views/error.py: 43%

34 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 07:15 +0000

1# Copyright © Michal Čihař <michal@weblate.org> 

2# 

3# SPDX-License-Identifier: GPL-3.0-or-later 

4from __future__ import annotations 

5 

6from typing import TYPE_CHECKING 

7 

8import django.views.defaults 

9import rest_framework.exceptions 

10from django.conf import settings 

11from django.middleware.csrf import REASON_NO_CSRF_COOKIE, REASON_NO_REFERER 

12from django.utils.translation import gettext 

13from sentry_sdk import last_event_id 

14 

15from weblate.trans.util import render 

16from weblate.utils.errors import report_error 

17 

18if TYPE_CHECKING: 18 ↛ 19line 18 didn't jump to line 19 because the condition on line 18 was never true

19 from weblate.auth.models import AuthenticatedHttpRequest 

20 

21 

22def bad_request(request: AuthenticatedHttpRequest, exception=None): 

23 """Error handler for bad request.""" 

24 if "text/html" not in request.headers.get("accept", ""): 

25 return rest_framework.exceptions.bad_request(request, exception) 

26 if exception: 

27 report_error("Bad request") 

28 return render(request, "400.html", {"title": gettext("Bad Request")}, status=400) 

29 

30 

31def not_found(request: AuthenticatedHttpRequest, exception=None): 

32 """Error handler showing list of available projects.""" 

33 return render(request, "404.html", {"title": gettext("Page Not Found")}, status=404) 

34 

35 

36def denied(request: AuthenticatedHttpRequest, exception=None): 

37 return render( 

38 request, "403.html", {"title": gettext("Permission Denied")}, status=403 

39 ) 

40 

41 

42def csrf_failure(request: AuthenticatedHttpRequest, reason=""): 

43 response = render( 

44 request, 

45 "403_csrf.html", 

46 { 

47 "title": gettext("Permission Denied"), 

48 "no_referer": reason == REASON_NO_REFERER, 

49 "no_cookie": reason == REASON_NO_CSRF_COOKIE, 

50 "reason": reason, 

51 }, 

52 status=403, 

53 ) 

54 # Avoid setting CSRF cookie on CSRF failure page, otherwise we end up creating 

55 # new session even when user might already have one (because browser did not 

56 # send the cookies with the CSRF request and Django doesn't see the session 

57 # cookie). 

58 response.csrf_cookie_set = True 

59 # Django 4.0+ 

60 request.META["CSRF_COOKIE_NEEDS_UPDATE"] = False 

61 return response 

62 

63 

64def server_error(request: AuthenticatedHttpRequest): 

65 """Error handler for server errors.""" 

66 if "text/html" not in request.headers.get("accept", ""): 

67 return rest_framework.exceptions.server_error(request) 

68 try: 

69 return render( 

70 request, 

71 "500.html", 

72 { 

73 "title": gettext("Internal Server Error"), 

74 "sentry_dsn": settings.SENTRY_DSN, 

75 "sentry_event_id": last_event_id(), 

76 }, 

77 status=500, 

78 ) 

79 except Exception: 

80 return django.views.defaults.server_error(request)