File: exceptions.py

package info (click to toggle)
strawberry-graphql-django 0.62.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,968 kB
  • sloc: python: 27,530; sh: 17; makefile: 16
file content (71 lines) | stat: -rw-r--r-- 2,448 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from __future__ import annotations

from functools import cached_property
from typing import TYPE_CHECKING

from strawberry.exceptions.exception import StrawberryException
from strawberry.exceptions.utils.source_finder import SourceFinder

if TYPE_CHECKING:
    from strawberry.exceptions.exception_source import ExceptionSource

    from strawberry_django.fields.filter_order import FilterOrderFieldResolver


class MissingFieldArgumentError(StrawberryException):
    def __init__(self, field_name: str, resolver: FilterOrderFieldResolver):
        self.function = resolver.wrapped_func

        self.message = f'Missing required argument "{field_name}" in "{resolver.name}"'
        self.rich_message = (
            f'[bold red]Missing argument [underline]"{field_name}" for field '
            f"`[underline]{resolver.name}[/]`"
        )
        self.annotation_message = "field missing argument"

        super().__init__(self.message)

    @cached_property
    def exception_source(self) -> ExceptionSource | None:  # pragma: no cover
        source_finder = SourceFinder()

        return source_finder.find_function_from_object(self.function)  # type: ignore


class ForbiddenFieldArgumentError(StrawberryException):
    def __init__(self, resolver: FilterOrderFieldResolver, arguments: list[str]):
        self.extra_arguments = arguments
        self.function = resolver.wrapped_func
        self.argument_name = arguments[0]

        self.message = (
            f'Found disallowed {self.extra_arguments_str} in field "{resolver.name}"'
        )
        self.rich_message = (
            f"Found disallowed {self.extra_arguments_str} in "
            f"`[underline]{resolver.name}[/]`"
        )
        self.suggestion = "To fix this error, remove offending argument(s)"

        self.annotation_message = "forbidden field argument"

        super().__init__(self.message)

    @property
    def extra_arguments_str(self) -> str:
        arguments = self.extra_arguments

        if len(arguments) == 1:
            return f'argument "{arguments[0]}"'

        head = ", ".join(arguments[:-1])
        return f'arguments "{head}" and "{arguments[-1]}"'

    @cached_property
    def exception_source(self) -> ExceptionSource | None:  # pragma: no cover
        source_finder = SourceFinder()

        return source_finder.find_argument_from_object(
            self.function,  # type: ignore
            self.argument_name,
        )