The server returned an error for a specific query.
| 59 | |
| 60 | |
| 61 | class QueryError(ClientError): |
| 62 | """The server returned an error for a specific query.""" |
| 63 | |
| 64 | _type = None |
| 65 | |
| 66 | def __new__(cls, errors: list[QueryErrorValue], *_): |
| 67 | error_types = { |
| 68 | subclass._type: subclass # noqa: SLF001 |
| 69 | for subclass in cls.__subclasses__() |
| 70 | if subclass._type # noqa: SLF001 |
| 71 | } |
| 72 | try: |
| 73 | new_type = error_types[errors[0].extensions["_type"]] |
| 74 | except (KeyError, IndexError): |
| 75 | return super().__new__(cls) |
| 76 | return super().__new__(new_type) |
| 77 | |
| 78 | def __init__(self, errors: list[QueryErrorValue], request: gql.GraphQLRequest): |
| 79 | if not errors: |
| 80 | msg = "Errors list is empty" |
| 81 | raise ValueError(msg) |
| 82 | super().__init__(*errors) |
| 83 | self.errors: list[QueryErrorValue] = errors |
| 84 | self.request = request |
| 85 | self.query = request.document |
| 86 | |
| 87 | @property |
| 88 | def error(self) -> QueryErrorValue: |
| 89 | return self.errors[0] |
| 90 | |
| 91 | def __str__(self) -> str: |
| 92 | return str(self.error) |
| 93 | |
| 94 | def debug_query(self): |
| 95 | """Return GraphQL query for debugging purposes. |
| 96 | |
| 97 | Example:: |
| 98 | |
| 99 | try: |
| 100 | await ctr |
| 101 | except dagger.QueryError as e: |
| 102 | print(e.debug_query()) |
| 103 | """ |
| 104 | lines = graphql.print_ast(self.query).splitlines() |
| 105 | # count number of digits from line count |
| 106 | pad = len(str(len(lines))) |
| 107 | locations = ( |
| 108 | {loc.line: loc.column for loc in self.errors[0].locations} |
| 109 | if self.errors[0].locations |
| 110 | else {} |
| 111 | ) |
| 112 | res = [] |
| 113 | for nr, line in enumerate(lines, start=1): |
| 114 | # prepend line number |
| 115 | res.append(f"{{:{pad}d}}: {{}}".format(nr, line)) |
| 116 | if nr in locations: |
| 117 | # add caret below line, pointing to start of error |
| 118 | res.append(" " * (pad + 1 + locations[nr]) + "^") |
no outgoing calls
no test coverage detected