exceptions.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. # Note: Any AirflowException raised is expected to cause the TaskInstance
  19. # to be marked in an ERROR state
  20. """Exceptions used by Airflow."""
  21. from __future__ import annotations
  22. import warnings
  23. from http import HTTPStatus
  24. from typing import TYPE_CHECKING, Any, NamedTuple
  25. from airflow.utils.trigger_rule import TriggerRule
  26. if TYPE_CHECKING:
  27. import datetime
  28. from collections.abc import Sized
  29. from airflow.models import DAG, DagRun
  30. class AirflowException(Exception):
  31. """
  32. Base class for all Airflow's errors.
  33. Each custom exception should be derived from this class.
  34. """
  35. status_code = HTTPStatus.INTERNAL_SERVER_ERROR
  36. def serialize(self):
  37. cls = self.__class__
  38. return f"{cls.__module__}.{cls.__name__}", (str(self),), {}
  39. class AirflowBadRequest(AirflowException):
  40. """Raise when the application or server cannot handle the request."""
  41. status_code = HTTPStatus.BAD_REQUEST
  42. class AirflowNotFoundException(AirflowException):
  43. """Raise when the requested object/resource is not available in the system."""
  44. status_code = HTTPStatus.NOT_FOUND
  45. class AirflowConfigException(AirflowException):
  46. """Raise when there is configuration problem."""
  47. class AirflowSensorTimeout(AirflowException):
  48. """Raise when there is a timeout on sensor polling."""
  49. class AirflowRescheduleException(AirflowException):
  50. """
  51. Raise when the task should be re-scheduled at a later time.
  52. :param reschedule_date: The date when the task should be rescheduled
  53. """
  54. def __init__(self, reschedule_date):
  55. super().__init__()
  56. self.reschedule_date = reschedule_date
  57. def serialize(self):
  58. cls = self.__class__
  59. return f"{cls.__module__}.{cls.__name__}", (), {"reschedule_date": self.reschedule_date}
  60. class InvalidStatsNameException(AirflowException):
  61. """Raise when name of the stats is invalid."""
  62. # Important to inherit BaseException instead of AirflowException->Exception, since this Exception is used
  63. # to explicitly interrupt ongoing task. Code that does normal error-handling should not treat
  64. # such interrupt as an error that can be handled normally. (Compare with KeyboardInterrupt)
  65. class AirflowTaskTimeout(BaseException):
  66. """Raise when the task execution times-out."""
  67. class AirflowTaskTerminated(BaseException):
  68. """Raise when the task execution is terminated."""
  69. class AirflowWebServerTimeout(AirflowException):
  70. """Raise when the web server times out."""
  71. class AirflowSkipException(AirflowException):
  72. """Raise when the task should be skipped."""
  73. class AirflowFailException(AirflowException):
  74. """Raise when the task should be failed without retrying."""
  75. class AirflowOptionalProviderFeatureException(AirflowException):
  76. """Raise by providers when imports are missing for optional provider features."""
  77. class AirflowInternalRuntimeError(BaseException):
  78. """
  79. Airflow Internal runtime error.
  80. Indicates that something really terrible happens during the Airflow execution.
  81. :meta private:
  82. """
  83. class XComNotFound(AirflowException):
  84. """Raise when an XCom reference is being resolved against a non-existent XCom."""
  85. def __init__(self, dag_id: str, task_id: str, key: str) -> None:
  86. super().__init__()
  87. self.dag_id = dag_id
  88. self.task_id = task_id
  89. self.key = key
  90. def __str__(self) -> str:
  91. return f'XComArg result from {self.task_id} at {self.dag_id} with key="{self.key}" is not found!'
  92. def serialize(self):
  93. cls = self.__class__
  94. return (
  95. f"{cls.__module__}.{cls.__name__}",
  96. (),
  97. {"dag_id": self.dag_id, "task_id": self.task_id, "key": self.key},
  98. )
  99. class UnmappableOperator(AirflowException):
  100. """Raise when an operator is not implemented to be mappable."""
  101. class XComForMappingNotPushed(AirflowException):
  102. """Raise when a mapped downstream's dependency fails to push XCom for task mapping."""
  103. def __str__(self) -> str:
  104. return "did not push XCom for task mapping"
  105. class UnmappableXComTypePushed(AirflowException):
  106. """Raise when an unmappable type is pushed as a mapped downstream's dependency."""
  107. def __init__(self, value: Any, *values: Any) -> None:
  108. super().__init__(value, *values)
  109. def __str__(self) -> str:
  110. typename = type(self.args[0]).__qualname__
  111. for arg in self.args[1:]:
  112. typename = f"{typename}[{type(arg).__qualname__}]"
  113. return f"unmappable return type {typename!r}"
  114. class UnmappableXComLengthPushed(AirflowException):
  115. """Raise when the pushed value is too large to map as a downstream's dependency."""
  116. def __init__(self, value: Sized, max_length: int) -> None:
  117. super().__init__(value)
  118. self.value = value
  119. self.max_length = max_length
  120. def __str__(self) -> str:
  121. return f"unmappable return value length: {len(self.value)} > {self.max_length}"
  122. class AirflowDagCycleException(AirflowException):
  123. """Raise when there is a cycle in DAG definition."""
  124. class AirflowDagDuplicatedIdException(AirflowException):
  125. """Raise when a DAG's ID is already used by another DAG."""
  126. def __init__(self, dag_id: str, incoming: str, existing: str) -> None:
  127. super().__init__(dag_id, incoming, existing)
  128. self.dag_id = dag_id
  129. self.incoming = incoming
  130. self.existing = existing
  131. def __str__(self) -> str:
  132. return f"Ignoring DAG {self.dag_id} from {self.incoming} - also found in {self.existing}"
  133. class AirflowDagInconsistent(AirflowException):
  134. """Raise when a DAG has inconsistent attributes."""
  135. class AirflowClusterPolicyViolation(AirflowException):
  136. """Raise when there is a violation of a Cluster Policy in DAG definition."""
  137. class AirflowClusterPolicySkipDag(AirflowException):
  138. """Raise when skipping dag is needed in Cluster Policy."""
  139. class AirflowClusterPolicyError(AirflowException):
  140. """Raise for a Cluster Policy other than AirflowClusterPolicyViolation or AirflowClusterPolicySkipDag."""
  141. class AirflowTimetableInvalid(AirflowException):
  142. """Raise when a DAG has an invalid timetable."""
  143. class DagNotFound(AirflowNotFoundException):
  144. """Raise when a DAG is not available in the system."""
  145. class DagCodeNotFound(AirflowNotFoundException):
  146. """Raise when a DAG code is not available in the system."""
  147. class DagRunNotFound(AirflowNotFoundException):
  148. """Raise when a DAG Run is not available in the system."""
  149. class DagRunAlreadyExists(AirflowBadRequest):
  150. """Raise when creating a DAG run for DAG which already has DAG run entry."""
  151. def __init__(self, dag_run: DagRun, execution_date: datetime.datetime, run_id: str) -> None:
  152. super().__init__(
  153. f"A DAG Run already exists for DAG {dag_run.dag_id} at {execution_date} with run id {run_id}"
  154. )
  155. self.dag_run = dag_run
  156. self.execution_date = execution_date
  157. self.run_id = run_id
  158. def serialize(self):
  159. cls = self.__class__
  160. # Note the DagRun object will be detached here and fails serialization, we need to create a new one
  161. from airflow.models import DagRun
  162. dag_run = DagRun(
  163. state=self.dag_run.state,
  164. dag_id=self.dag_run.dag_id,
  165. run_id=self.dag_run.run_id,
  166. external_trigger=self.dag_run.external_trigger,
  167. run_type=self.dag_run.run_type,
  168. execution_date=self.dag_run.execution_date,
  169. )
  170. dag_run.id = self.dag_run.id
  171. return (
  172. f"{cls.__module__}.{cls.__name__}",
  173. (),
  174. {"dag_run": dag_run, "execution_date": self.execution_date, "run_id": self.run_id},
  175. )
  176. class DagFileExists(AirflowBadRequest):
  177. """Raise when a DAG ID is still in DagBag i.e., DAG file is in DAG folder."""
  178. def __init__(self, *args, **kwargs):
  179. super().__init__(*args, **kwargs)
  180. warnings.warn("DagFileExists is deprecated and will be removed.", DeprecationWarning, stacklevel=2)
  181. class FailStopDagInvalidTriggerRule(AirflowException):
  182. """Raise when a dag has 'fail_stop' enabled yet has a non-default trigger rule."""
  183. _allowed_rules = (TriggerRule.ALL_SUCCESS, TriggerRule.ALL_DONE_SETUP_SUCCESS)
  184. @classmethod
  185. def check(cls, *, dag: DAG | None, trigger_rule: TriggerRule):
  186. """
  187. Check that fail_stop dag tasks have allowable trigger rules.
  188. :meta private:
  189. """
  190. if dag is not None and dag.fail_stop and trigger_rule not in cls._allowed_rules:
  191. raise cls()
  192. def __str__(self) -> str:
  193. return f"A 'fail-stop' dag can only have {TriggerRule.ALL_SUCCESS} trigger rule"
  194. class DuplicateTaskIdFound(AirflowException):
  195. """Raise when a Task with duplicate task_id is defined in the same DAG."""
  196. class TaskAlreadyInTaskGroup(AirflowException):
  197. """Raise when a Task cannot be added to a TaskGroup since it already belongs to another TaskGroup."""
  198. def __init__(self, task_id: str, existing_group_id: str | None, new_group_id: str) -> None:
  199. super().__init__(task_id, new_group_id)
  200. self.task_id = task_id
  201. self.existing_group_id = existing_group_id
  202. self.new_group_id = new_group_id
  203. def __str__(self) -> str:
  204. if self.existing_group_id is None:
  205. existing_group = "the DAG's root group"
  206. else:
  207. existing_group = f"group {self.existing_group_id!r}"
  208. return f"cannot add {self.task_id!r} to {self.new_group_id!r} (already in {existing_group})"
  209. class SerializationError(AirflowException):
  210. """A problem occurred when trying to serialize something."""
  211. class ParamValidationError(AirflowException):
  212. """Raise when DAG params is invalid."""
  213. class TaskNotFound(AirflowNotFoundException):
  214. """Raise when a Task is not available in the system."""
  215. class TaskInstanceNotFound(AirflowNotFoundException):
  216. """Raise when a task instance is not available in the system."""
  217. class PoolNotFound(AirflowNotFoundException):
  218. """Raise when a Pool is not available in the system."""
  219. class NoAvailablePoolSlot(AirflowException):
  220. """Raise when there is not enough slots in pool."""
  221. class DagConcurrencyLimitReached(AirflowException):
  222. """Raise when DAG max_active_tasks limit is reached."""
  223. class TaskConcurrencyLimitReached(AirflowException):
  224. """Raise when task max_active_tasks limit is reached."""
  225. class BackfillUnfinished(AirflowException):
  226. """
  227. Raises when not all tasks succeed in backfill.
  228. :param message: The human-readable description of the exception
  229. :param ti_status: The information about all task statuses
  230. """
  231. def __init__(self, message, ti_status):
  232. super().__init__(message)
  233. self.ti_status = ti_status
  234. class FileSyntaxError(NamedTuple):
  235. """Information about a single error in a file."""
  236. line_no: int | None
  237. message: str
  238. def __str__(self):
  239. return f"{self.message}. Line number: s{str(self.line_no)},"
  240. class AirflowFileParseException(AirflowException):
  241. """
  242. Raises when connection or variable file can not be parsed.
  243. :param msg: The human-readable description of the exception
  244. :param file_path: A processed file that contains errors
  245. :param parse_errors: File syntax errors
  246. """
  247. def __init__(self, msg: str, file_path: str, parse_errors: list[FileSyntaxError]) -> None:
  248. super().__init__(msg)
  249. self.msg = msg
  250. self.file_path = file_path
  251. self.parse_errors = parse_errors
  252. def __str__(self):
  253. from airflow.utils.code_utils import prepare_code_snippet
  254. from airflow.utils.platform import is_tty
  255. result = f"{self.msg}\nFilename: {self.file_path}\n\n"
  256. for error_no, parse_error in enumerate(self.parse_errors, 1):
  257. result += "=" * 20 + f" Parse error {error_no:3} " + "=" * 20 + "\n"
  258. result += f"{parse_error.message}\n"
  259. if parse_error.line_no:
  260. result += f"Line number: {parse_error.line_no}\n"
  261. if parse_error.line_no and is_tty():
  262. result += "\n" + prepare_code_snippet(self.file_path, parse_error.line_no) + "\n"
  263. return result
  264. class ConnectionNotUnique(AirflowException):
  265. """Raise when multiple values are found for the same connection ID."""
  266. class TaskDeferred(BaseException):
  267. """
  268. Signal an operator moving to deferred state.
  269. Special exception raised to signal that the operator it was raised from
  270. wishes to defer until a trigger fires. Triggers can send execution back to task or end the task instance
  271. directly. If the trigger should end the task instance itself, ``method_name`` does not matter,
  272. and can be None; otherwise, provide the name of the method that should be used when
  273. resuming execution in the task.
  274. """
  275. def __init__(
  276. self,
  277. *,
  278. trigger,
  279. method_name: str,
  280. kwargs: dict[str, Any] | None = None,
  281. timeout: datetime.timedelta | None = None,
  282. ):
  283. super().__init__()
  284. self.trigger = trigger
  285. self.method_name = method_name
  286. self.kwargs = kwargs
  287. self.timeout = timeout
  288. # Check timeout type at runtime
  289. if self.timeout is not None and not hasattr(self.timeout, "total_seconds"):
  290. raise ValueError("Timeout value must be a timedelta")
  291. def serialize(self):
  292. cls = self.__class__
  293. return (
  294. f"{cls.__module__}.{cls.__name__}",
  295. (),
  296. {
  297. "trigger": self.trigger,
  298. "method_name": self.method_name,
  299. "kwargs": self.kwargs,
  300. "timeout": self.timeout,
  301. },
  302. )
  303. def __repr__(self) -> str:
  304. return f"<TaskDeferred trigger={self.trigger} method={self.method_name}>"
  305. class TaskDeferralError(AirflowException):
  306. """Raised when a task failed during deferral for some reason."""
  307. # The try/except handling is needed after we moved all k8s classes to cncf.kubernetes provider
  308. # These two exceptions are used internally by Kubernetes Executor but also by PodGenerator, so we need
  309. # to leave them here in case older version of cncf.kubernetes provider is used to run KubernetesPodOperator
  310. # and it raises one of those exceptions. The code should be backwards compatible even if you import
  311. # and try/except the exception using direct imports from airflow.exceptions.
  312. # 1) if you have old provider, both provider and pod generator will throw the "airflow.exceptions" exception.
  313. # 2) if you have new provider, both provider and pod generator will throw the
  314. # "airflow.providers.cncf.kubernetes" as it will be imported here from the provider.
  315. try:
  316. from airflow.providers.cncf.kubernetes.pod_generator import PodMutationHookException
  317. except ImportError:
  318. class PodMutationHookException(AirflowException): # type: ignore[no-redef]
  319. """Raised when exception happens during Pod Mutation Hook execution."""
  320. try:
  321. from airflow.providers.cncf.kubernetes.pod_generator import PodReconciliationError
  322. except ImportError:
  323. class PodReconciliationError(AirflowException): # type: ignore[no-redef]
  324. """Raised when an error is encountered while trying to merge pod configs."""
  325. class RemovedInAirflow3Warning(DeprecationWarning):
  326. """Issued for usage of deprecated features that will be removed in Airflow3."""
  327. deprecated_since: str | None = None
  328. "Indicates the airflow version that started raising this deprecation warning"
  329. class AirflowProviderDeprecationWarning(DeprecationWarning):
  330. """Issued for usage of deprecated features of Airflow provider."""
  331. deprecated_provider_since: str | None = None
  332. "Indicates the provider version that started raising this deprecation warning"
  333. class DeserializingResultError(ValueError):
  334. """Raised when an error is encountered while a pickling library deserializes a pickle file."""
  335. def __str__(self):
  336. return (
  337. "Error deserializing result. Note that result deserialization "
  338. "is not supported across major Python versions. Cause: " + str(self.__cause__)
  339. )
  340. class UnknownExecutorException(ValueError):
  341. """Raised when an attempt is made to load an executor which is not configured."""