Edit on GitHub

# sqlglot.errors

View Source

     1from __future__ import annotations
     2
     3import typing as t
     4from enum import auto
     5
     6from sqlglot.helper import AutoName
     7
     8
     9class ErrorLevel(AutoName):
    10    IGNORE = auto()
    11    """Ignore all errors."""
    12
    13    WARN = auto()
    14    """Log all errors."""
    15
    16    RAISE = auto()
    17    """Collect all errors and raise a single exception."""
    18
    19    IMMEDIATE = auto()
    20    """Immediately raise an exception on the first error found."""
    21
    22
    23class SqlglotError(Exception):
    24    pass
    25
    26
    27class UnsupportedError(SqlglotError):
    28    pass
    29
    30
    31class ParseError(SqlglotError):
    32    def __init__(
    33        self,
    34        message: str,
    35        errors: t.Optional[t.List[t.Dict[str, t.Any]]] = None,
    36    ):
    37        super().__init__(message)
    38        self.errors = errors or []
    39
    40    @classmethod
    41    def new(
    42        cls,
    43        message: str,
    44        description: t.Optional[str] = None,
    45        line: t.Optional[int] = None,
    46        col: t.Optional[int] = None,
    47        start_context: t.Optional[str] = None,
    48        highlight: t.Optional[str] = None,
    49        end_context: t.Optional[str] = None,
    50        into_expression: t.Optional[str] = None,
    51    ) -> ParseError:
    52        return cls(
    53            message,
    54            [
    55                {
    56                    "description": description,
    57                    "line": line,
    58                    "col": col,
    59                    "start_context": start_context,
    60                    "highlight": highlight,
    61                    "end_context": end_context,
    62                    "into_expression": into_expression,
    63                }
    64            ],
    65        )
    66
    67
    68class TokenError(SqlglotError):
    69    pass
    70
    71
    72class OptimizeError(SqlglotError):
    73    pass
    74
    75
    76class SchemaError(SqlglotError):
    77    pass
    78
    79
    80class ExecuteError(SqlglotError):
    81    pass
    82
    83
    84def concat_messages(errors: t.Sequence[t.Any], maximum: int) -> str:
    85    msg = [str(e) for e in errors[:maximum]]
    86    remaining = len(errors) - maximum
    87    if remaining > 0:
    88        msg.append(f"... and {remaining} more")
    89    return "\n\n".join(msg)
    90
    91
    92def merge_errors(errors: t.Sequence[ParseError]) -> t.List[t.Dict[str, t.Any]]:
    93    return [e_dict for error in errors for e_dict in error.errors]

class ErrorLevel(sqlglot.helper.AutoName): View Source

    10class ErrorLevel(AutoName):
    11    IGNORE = auto()
    12    """Ignore all errors."""
    13
    14    WARN = auto()
    15    """Log all errors."""
    16
    17    RAISE = auto()
    18    """Collect all errors and raise a single exception."""
    19
    20    IMMEDIATE = auto()
    21    """Immediately raise an exception on the first error found."""

An enumeration.

IGNORE = <ErrorLevel.IGNORE: 'IGNORE'>

Ignore all errors.

WARN = <ErrorLevel.WARN: 'WARN'>

Log all errors.

RAISE = <ErrorLevel.RAISE: 'RAISE'>

Collect all errors and raise a single exception.

IMMEDIATE = <ErrorLevel.IMMEDIATE: 'IMMEDIATE'>

Immediately raise an exception on the first error found.

class SqlglotError(builtins.Exception): View Source

    24class SqlglotError(Exception):
    25    pass

Common base class for all non-exit exceptions.

class UnsupportedError(SqlglotError): View Source

    28class UnsupportedError(SqlglotError):
    29    pass

Common base class for all non-exit exceptions.

class ParseError(SqlglotError): View Source

    32class ParseError(SqlglotError):
    33    def __init__(
    34        self,
    35        message: str,
    36        errors: t.Optional[t.List[t.Dict[str, t.Any]]] = None,
    37    ):
    38        super().__init__(message)
    39        self.errors = errors or []
    40
    41    @classmethod
    42    def new(
    43        cls,
    44        message: str,
    45        description: t.Optional[str] = None,
    46        line: t.Optional[int] = None,
    47        col: t.Optional[int] = None,
    48        start_context: t.Optional[str] = None,
    49        highlight: t.Optional[str] = None,
    50        end_context: t.Optional[str] = None,
    51        into_expression: t.Optional[str] = None,
    52    ) -> ParseError:
    53        return cls(
    54            message,
    55            [
    56                {
    57                    "description": description,
    58                    "line": line,
    59                    "col": col,
    60                    "start_context": start_context,
    61                    "highlight": highlight,
    62                    "end_context": end_context,
    63                    "into_expression": into_expression,
    64                }
    65            ],
    66        )

Common base class for all non-exit exceptions.

ParseError(message: str, errors: Optional[List[Dict[str, Any]]] = None) View Source

    33    def __init__(
    34        self,
    35        message: str,
    36        errors: t.Optional[t.List[t.Dict[str, t.Any]]] = None,
    37    ):
    38        super().__init__(message)
    39        self.errors = errors or []

errors
@classmethod
def new( cls, message: str, description: Optional[str] = None, line: Optional[int] = None, col: Optional[int] = None, start_context: Optional[str] = None, highlight: Optional[str] = None, end_context: Optional[str] = None, into_expression: Optional[str] = None) -> ParseError: View Source

    41    @classmethod
    42    def new(
    43        cls,
    44        message: str,
    45        description: t.Optional[str] = None,
    46        line: t.Optional[int] = None,
    47        col: t.Optional[int] = None,
    48        start_context: t.Optional[str] = None,
    49        highlight: t.Optional[str] = None,
    50        end_context: t.Optional[str] = None,
    51        into_expression: t.Optional[str] = None,
    52    ) -> ParseError:
    53        return cls(
    54            message,
    55            [
    56                {
    57                    "description": description,
    58                    "line": line,
    59                    "col": col,
    60                    "start_context": start_context,
    61                    "highlight": highlight,
    62                    "end_context": end_context,
    63                    "into_expression": into_expression,
    64                }
    65            ],
    66        )

class TokenError(SqlglotError): View Source

    69class TokenError(SqlglotError):
    70    pass

Common base class for all non-exit exceptions.

class OptimizeError(SqlglotError): View Source

    73class OptimizeError(SqlglotError):
    74    pass

Common base class for all non-exit exceptions.

class SchemaError(SqlglotError): View Source

    77class SchemaError(SqlglotError):
    78    pass

Common base class for all non-exit exceptions.

class ExecuteError(SqlglotError): View Source

    81class ExecuteError(SqlglotError):
    82    pass

Common base class for all non-exit exceptions.

def concat_messages(errors: Sequence[Any], maximum: int) -> str: View Source

    85def concat_messages(errors: t.Sequence[t.Any], maximum: int) -> str:
    86    msg = [str(e) for e in errors[:maximum]]
    87    remaining = len(errors) - maximum
    88    if remaining > 0:
    89        msg.append(f"... and {remaining} more")
    90    return "\n\n".join(msg)

def merge_errors(errors: Sequence[ParseError]) -> List[Dict[str, Any]]: View Source

    93def merge_errors(errors: t.Sequence[ParseError]) -> t.List[t.Dict[str, t.Any]]:
    94    return [e_dict for error in errors for e_dict in error.errors]