• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rafalp / Misago / 10098665926

25 Jul 2024 05:24PM UTC coverage: 97.256% (-0.5%) from 97.748%
10098665926

Pull #1758

github

web-flow
Merge 28f4372ae into 71a73d2d5
Pull Request #1758: Redo threads lists

3098 of 3362 new or added lines in 106 files covered. (92.15%)

93 existing lines in 16 files now uncovered.

55176 of 56733 relevant lines covered (97.26%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/misago/core/exceptionhandler.py
1
from urllib.parse import urlencode
1✔
2

3
from django.core.exceptions import PermissionDenied
1✔
4
from django.http import (
1✔
5
    Http404,
6
    HttpResponse,
7
    HttpResponsePermanentRedirect,
8
    JsonResponse,
9
)
10
from django.urls import reverse
1✔
11
from rest_framework.views import exception_handler as rest_exception_handler
1✔
12
from social_core.exceptions import SocialAuthBaseException
1✔
13
from social_core.utils import social_logger
1✔
14

15
from . import errorpages
1✔
16
from .exceptions import AjaxError, Banned, ExplicitFirstPage, OutdatedSlug
1✔
17

18
HANDLED_EXCEPTIONS = (
1✔
19
    AjaxError,
20
    Banned,
21
    ExplicitFirstPage,
22
    Http404,
23
    OutdatedSlug,
24
    PermissionDenied,
25
    SocialAuthBaseException,
26
)
27

28

29
def is_misago_exception(exception):
1✔
30
    return isinstance(exception, HANDLED_EXCEPTIONS)
1✔
31

32

33
def handle_ajax_error(request, exception):
1✔
34
    json = {"is_error": 1, "message": str(exception.message)}
×
35
    return JsonResponse(json, status=exception.code)
×
36

37

38
def handle_banned_exception(request, exception):
1✔
39
    return errorpages.banned(request, exception)
1✔
40

41

42
def handle_explicit_first_page_exception(request, exception):
1✔
43
    matched_url = request.resolver_match.url_name
1✔
44
    if request.resolver_match.namespace:
1✔
45
        matched_url = "%s:%s" % (request.resolver_match.namespace, matched_url)
×
46

47
    url_kwargs = request.resolver_match.kwargs
1✔
48
    del url_kwargs["page"]
1✔
49

50
    new_url = reverse(matched_url, kwargs=url_kwargs)
1✔
51
    return HttpResponsePermanentRedirect(new_url)
1✔
52

53

54
def handle_http404_exception(request, exception):
1✔
55
    return errorpages.page_not_found(request, exception)
1✔
56

57

58
def handle_outdated_slug_exception(request, exception):
1✔
59
    view_name = request.resolver_match.view_name
1✔
60

61
    model = exception.args[0]
1✔
62
    url_kwargs = request.resolver_match.kwargs
1✔
63
    url_kwargs["slug"] = model.slug
1✔
64

65
    new_url = reverse(view_name, kwargs=url_kwargs)
1✔
66
    if request.GET:
1✔
NEW
67
        new_url += "?" + urlencode(request.GET)
×
68

69
    return HttpResponsePermanentRedirect(new_url)
1✔
70

71

72
def handle_permission_denied_exception(request, exception):
1✔
73
    return errorpages.permission_denied(request, exception)
1✔
74

75

76
def handle_social_auth_exception(request, exception):
1✔
77
    social_logger.error(exception, exc_info=exception)
1✔
78
    return errorpages.social_auth_failed(request, exception)
1✔
79

80

81
EXCEPTION_HANDLERS = [
1✔
82
    (AjaxError, handle_ajax_error),
83
    (Banned, handle_banned_exception),
84
    (Http404, handle_http404_exception),
85
    (ExplicitFirstPage, handle_explicit_first_page_exception),
86
    (OutdatedSlug, handle_outdated_slug_exception),
87
    (PermissionDenied, handle_permission_denied_exception),
88
    (SocialAuthBaseException, handle_social_auth_exception),
89
]
90

91

92
def get_exception_handler(exception):
1✔
93
    for exception_type, handler in EXCEPTION_HANDLERS:
1✔
94
        if isinstance(exception, exception_type):
1✔
95
            return handler
1✔
96
    raise ValueError("%s is not a Misago exception" % exception.__class__.__name__)
1✔
97

98

99
def handle_misago_exception(request, exception):
1✔
100
    if request.is_htmx and isinstance(exception, (Http404, PermissionDenied)):
1✔
101
        return handle_htmx_exception(exception)
1✔
102

103
    handler = get_exception_handler(exception)
1✔
104
    return handler(request, exception)
1✔
105

106

107
def handle_htmx_exception(exception: Http404 | PermissionDenied) -> HttpResponse:
1✔
108
    status = status = 404 if isinstance(exception, Http404) else 403
1✔
109
    if not exception.args:
1✔
110
        return HttpResponse(status=status)
1✔
111

112
    return JsonResponse({"error": str(exception.args[0])}, status=status)
1✔
113

114

115
def handle_api_exception(exception, context):
1✔
116
    response = rest_exception_handler(exception, context)
1✔
117
    if response:
1✔
118
        if isinstance(exception, Banned):
1✔
119
            response.data["ban"] = exception.ban.get_serialized_message()
1✔
120
        elif isinstance(exception, PermissionDenied):
1✔
121
            try:
1✔
122
                response.data["detail"] = exception.args[0]
1✔
123
            except IndexError:
1✔
124
                pass
1✔
125
        return response
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc