Edit on GitHub

sqlmesh.utils.errors

  1from __future__ import annotations
  2
  3import typing as t
  4from enum import auto
  5from pathlib import Path
  6
  7from sqlglot import exp
  8from sqlglot.helper import AutoName
  9
 10if t.TYPE_CHECKING:
 11    from requests.models import Response
 12
 13    from sqlmesh.core.model import Model
 14
 15
 16class ErrorLevel(AutoName):
 17    IGNORE = auto()
 18    WARN = auto()
 19    RAISE = auto()
 20
 21
 22class SQLMeshError(Exception):
 23    pass
 24
 25
 26class ConfigError(SQLMeshError):
 27    pass
 28
 29
 30class MissingDependencyError(SQLMeshError):
 31    """Local environment is missing a required dependency for the given operation"""
 32
 33
 34class MacroEvalError(SQLMeshError):
 35    pass
 36
 37
 38class PlanError(SQLMeshError):
 39    pass
 40
 41
 42class NoChangesPlanError(PlanError):
 43    pass
 44
 45
 46class UncategorizedPlanError(PlanError):
 47    pass
 48
 49
 50class MissingContextException(Exception):
 51    pass
 52
 53
 54class SnapshotVersionError(SQLMeshError):
 55    pass
 56
 57
 58class MagicError(SQLMeshError):
 59    pass
 60
 61
 62class AuditConfigError(ConfigError):
 63    pass
 64
 65
 66class AuditError(SQLMeshError):
 67    def __init__(
 68        self,
 69        audit_name: str,
 70        count: int,
 71        query: exp.Query,
 72        model: t.Optional[Model] = None,
 73        # the dialect of the engine adapter that evaluated the audit query
 74        adapter_dialect: t.Optional[str] = None,
 75    ) -> None:
 76        self.audit_name = audit_name
 77        self.model = model
 78        self.count = count
 79        self.query = query
 80        self.adapter_dialect = adapter_dialect
 81
 82    def __str__(self) -> str:
 83        model_str = f" for model '{self.model_name}'" if self.model_name else ""
 84        return f"Audit '{self.audit_name}'{model_str} failed.\nGot {self.count} results, expected 0.\n{self.sql()}"
 85
 86    @property
 87    def model_name(self) -> t.Optional[str]:
 88        return self.model.name if self.model else None
 89
 90    def sql(self, dialect: t.Optional[str] = None, **opts: t.Any) -> str:
 91        """
 92        Returns the rendered audit query that failed.
 93
 94        Args:
 95            dialect: the dialect of the output SQL string, by default,
 96                     this will use the dialect of the engine adapter that ran the query.
 97            opts: other `sqlglot.generator.Generator` options.
 98
 99        Returns:
100            The SQL string.
101        """
102        return self.query.sql(dialect=dialect or self.adapter_dialect, **opts)
103
104
105class TestError(SQLMeshError):
106    pass
107
108
109class NotificationTargetError(SQLMeshError):
110    pass
111
112
113class ApiError(SQLMeshError):
114    def __init__(self, message: str, code: int) -> None:
115        super().__init__(message)
116        self.code = code
117
118
119class ApiClientError(ApiError):
120    pass
121
122
123class ApiServerError(ApiError):
124    pass
125
126
127class NotFoundError(ApiClientError):
128    def __init__(self, message: str) -> None:
129        super().__init__(message, 404)
130
131
132class CICDBotError(SQLMeshError):
133    pass
134
135
136class ParsetimeAdapterCallError(SQLMeshError):
137    pass
138
139
140class EngineAdapterError(SQLMeshError):
141    pass
142
143
144class UnsupportedCatalogOperationError(EngineAdapterError):
145    pass
146
147
148class CircuitBreakerError(SQLMeshError):
149    def __init__(self) -> None:
150        super().__init__("Circuit breaker has been triggered.")
151
152
153def raise_config_error(
154    msg: str,
155    location: t.Optional[str | Path] = None,
156    error_type: t.Type[ConfigError] = ConfigError,
157) -> None:
158    if location:
159        raise error_type(f"{msg} at '{location}'")
160    raise error_type(msg)
161
162
163def raise_for_status(response: Response) -> None:
164    if response.status_code == 404:
165        raise NotFoundError(response.text)
166    if 400 <= response.status_code < 500:
167        raise ApiClientError(response.text, response.status_code)
168    if 500 <= response.status_code < 600:
169        raise ApiServerError(response.text, response.status_code)
class ErrorLevel(sqlglot.helper.AutoName):
17class ErrorLevel(AutoName):
18    IGNORE = auto()
19    WARN = auto()
20    RAISE = auto()

An enumeration.

IGNORE = <ErrorLevel.IGNORE: 'IGNORE'>
WARN = <ErrorLevel.WARN: 'WARN'>
RAISE = <ErrorLevel.RAISE: 'RAISE'>
Inherited Members
enum.Enum
name
value
class SQLMeshError(builtins.Exception):
23class SQLMeshError(Exception):
24    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class ConfigError(SQLMeshError):
27class ConfigError(SQLMeshError):
28    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class MissingDependencyError(SQLMeshError):
31class MissingDependencyError(SQLMeshError):
32    """Local environment is missing a required dependency for the given operation"""

Local environment is missing a required dependency for the given operation

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class MacroEvalError(SQLMeshError):
35class MacroEvalError(SQLMeshError):
36    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class PlanError(SQLMeshError):
39class PlanError(SQLMeshError):
40    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class NoChangesPlanError(PlanError):
43class NoChangesPlanError(PlanError):
44    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class UncategorizedPlanError(PlanError):
47class UncategorizedPlanError(PlanError):
48    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class MissingContextException(builtins.Exception):
51class MissingContextException(Exception):
52    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class SnapshotVersionError(SQLMeshError):
55class SnapshotVersionError(SQLMeshError):
56    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class MagicError(SQLMeshError):
59class MagicError(SQLMeshError):
60    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class AuditConfigError(ConfigError):
63class AuditConfigError(ConfigError):
64    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class AuditError(SQLMeshError):
 67class AuditError(SQLMeshError):
 68    def __init__(
 69        self,
 70        audit_name: str,
 71        count: int,
 72        query: exp.Query,
 73        model: t.Optional[Model] = None,
 74        # the dialect of the engine adapter that evaluated the audit query
 75        adapter_dialect: t.Optional[str] = None,
 76    ) -> None:
 77        self.audit_name = audit_name
 78        self.model = model
 79        self.count = count
 80        self.query = query
 81        self.adapter_dialect = adapter_dialect
 82
 83    def __str__(self) -> str:
 84        model_str = f" for model '{self.model_name}'" if self.model_name else ""
 85        return f"Audit '{self.audit_name}'{model_str} failed.\nGot {self.count} results, expected 0.\n{self.sql()}"
 86
 87    @property
 88    def model_name(self) -> t.Optional[str]:
 89        return self.model.name if self.model else None
 90
 91    def sql(self, dialect: t.Optional[str] = None, **opts: t.Any) -> str:
 92        """
 93        Returns the rendered audit query that failed.
 94
 95        Args:
 96            dialect: the dialect of the output SQL string, by default,
 97                     this will use the dialect of the engine adapter that ran the query.
 98            opts: other `sqlglot.generator.Generator` options.
 99
100        Returns:
101            The SQL string.
102        """
103        return self.query.sql(dialect=dialect or self.adapter_dialect, **opts)

Common base class for all non-exit exceptions.

AuditError( audit_name: str, count: int, query: sqlglot.expressions.Query, model: Union[sqlmesh.core.model.definition.SqlModel, sqlmesh.core.model.definition.SeedModel, sqlmesh.core.model.definition.PythonModel, sqlmesh.core.model.definition.ExternalModel, NoneType] = None, adapter_dialect: Union[str, NoneType] = None)
68    def __init__(
69        self,
70        audit_name: str,
71        count: int,
72        query: exp.Query,
73        model: t.Optional[Model] = None,
74        # the dialect of the engine adapter that evaluated the audit query
75        adapter_dialect: t.Optional[str] = None,
76    ) -> None:
77        self.audit_name = audit_name
78        self.model = model
79        self.count = count
80        self.query = query
81        self.adapter_dialect = adapter_dialect
def sql(self, dialect: Union[str, NoneType] = None, **opts: Any) -> str:
 91    def sql(self, dialect: t.Optional[str] = None, **opts: t.Any) -> str:
 92        """
 93        Returns the rendered audit query that failed.
 94
 95        Args:
 96            dialect: the dialect of the output SQL string, by default,
 97                     this will use the dialect of the engine adapter that ran the query.
 98            opts: other `sqlglot.generator.Generator` options.
 99
100        Returns:
101            The SQL string.
102        """
103        return self.query.sql(dialect=dialect or self.adapter_dialect, **opts)

Returns the rendered audit query that failed.

Arguments:
  • dialect: the dialect of the output SQL string, by default, this will use the dialect of the engine adapter that ran the query.
  • opts: other sqlglot.generator.Generator options.
Returns:

The SQL string.

Inherited Members
builtins.BaseException
with_traceback
class TestError(SQLMeshError):
106class TestError(SQLMeshError):
107    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class NotificationTargetError(SQLMeshError):
110class NotificationTargetError(SQLMeshError):
111    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class ApiError(SQLMeshError):
114class ApiError(SQLMeshError):
115    def __init__(self, message: str, code: int) -> None:
116        super().__init__(message)
117        self.code = code

Common base class for all non-exit exceptions.

ApiError(message: str, code: int)
115    def __init__(self, message: str, code: int) -> None:
116        super().__init__(message)
117        self.code = code
Inherited Members
builtins.BaseException
with_traceback
class ApiClientError(ApiError):
120class ApiClientError(ApiError):
121    pass

Common base class for all non-exit exceptions.

Inherited Members
ApiError
ApiError
builtins.BaseException
with_traceback
class ApiServerError(ApiError):
124class ApiServerError(ApiError):
125    pass

Common base class for all non-exit exceptions.

Inherited Members
ApiError
ApiError
builtins.BaseException
with_traceback
class NotFoundError(ApiClientError):
128class NotFoundError(ApiClientError):
129    def __init__(self, message: str) -> None:
130        super().__init__(message, 404)

Common base class for all non-exit exceptions.

NotFoundError(message: str)
129    def __init__(self, message: str) -> None:
130        super().__init__(message, 404)
Inherited Members
builtins.BaseException
with_traceback
class CICDBotError(SQLMeshError):
133class CICDBotError(SQLMeshError):
134    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class ParsetimeAdapterCallError(SQLMeshError):
137class ParsetimeAdapterCallError(SQLMeshError):
138    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class EngineAdapterError(SQLMeshError):
141class EngineAdapterError(SQLMeshError):
142    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class UnsupportedCatalogOperationError(EngineAdapterError):
145class UnsupportedCatalogOperationError(EngineAdapterError):
146    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
class CircuitBreakerError(SQLMeshError):
149class CircuitBreakerError(SQLMeshError):
150    def __init__(self) -> None:
151        super().__init__("Circuit breaker has been triggered.")

Common base class for all non-exit exceptions.

Inherited Members
builtins.BaseException
with_traceback
def raise_config_error( msg: str, location: 't.Optional[str | Path]' = None, error_type: Type[sqlmesh.utils.errors.ConfigError] = <class 'sqlmesh.utils.errors.ConfigError'>) -> None:
154def raise_config_error(
155    msg: str,
156    location: t.Optional[str | Path] = None,
157    error_type: t.Type[ConfigError] = ConfigError,
158) -> None:
159    if location:
160        raise error_type(f"{msg} at '{location}'")
161    raise error_type(msg)
def raise_for_status(response: requests.models.Response) -> None:
164def raise_for_status(response: Response) -> None:
165    if response.status_code == 404:
166        raise NotFoundError(response.text)
167    if 400 <= response.status_code < 500:
168        raise ApiClientError(response.text, response.status_code)
169    if 500 <= response.status_code < 600:
170        raise ApiServerError(response.text, response.status_code)