File: exceptions.py

package info (click to toggle)
python-gjson 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 348 kB
  • sloc: python: 2,034; makefile: 20
file content (38 lines) | stat: -rw-r--r-- 1,313 bytes parent folder | download | duplicates (3)
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
"""gjson custom exceptions module."""
from typing import Any


class GJSONError(Exception):
    """Raised by the gjson module on error while performing queries or converting to JSON."""


class GJSONParseError(GJSONError):
    """Raised when there is an error parsing the query string, with nicer representation of the error."""

    def __init__(self, *args: Any, query: str, position: int):
        """Initialize the exception with the additional data of the query part.

        Arguments:
            *args: all positional arguments like any regular exception.
            query: the full query that generated the parse error.
            position: the position in the query string where the parse error occurred.

        """
        super().__init__(*args)
        self.query = query
        self.position = position

    def __str__(self) -> str:
        """Return a custom representation of the error.

        Returns:
            the whole query string with a clear indication on where the error occurred.

        """
        default = super().__str__()
        line = '-' * (self.position + 7)  # 7 is for the lenght of 'Query: '
        return f'{default}\nQuery: {self.query}\n{line}^'


class GJSONInvalidSyntaxError(GJSONParseError):
    """Raised when there is a query with an invalid syntax."""