## Licensed to the Apache Software Foundation (ASF) under one or more# contributor license agreements. See the NOTICE file distributed with# this work for additional information regarding copyright ownership.# The ASF licenses this file to You under the Apache License, Version 2.0# (the "License"); you may not use this file except in compliance with# the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.#fromabcimportABC,abstractmethodfromenumimportEnumfromtypingimportDict,Optional,cast,Iterable,TYPE_CHECKING,Listfrompyspark.errors.utilsimportErrorClassesReaderfrompyspark.loggerimportPySparkLoggerfrompickleimportPicklingErrorifTYPE_CHECKING:frompyspark.sql.typesimportRow
[docs]classPySparkException(Exception):""" Base Exception for handling errors generated from PySpark. """def__init__(self,message:Optional[str]=None,errorClass:Optional[str]=None,messageParameters:Optional[Dict[str,str]]=None,contexts:Optional[List["QueryContext"]]=None,):ifcontextsisNone:contexts=[]self._error_reader=ErrorClassesReader()ifmessageisNone:self._message=self._error_reader.get_error_message(cast(str,errorClass),cast(Dict[str,str],messageParameters))else:self._message=messageself._errorClass=errorClassself._messageParameters=messageParametersself._contexts=contexts
[docs]defgetErrorClass(self)->Optional[str]:""" Returns an error class as a string. .. versionadded:: 3.4.0 See Also -------- :meth:`PySparkException.getMessage` :meth:`PySparkException.getMessageParameters` :meth:`PySparkException.getQueryContext` :meth:`PySparkException.getSqlState` """returnself._errorClass
[docs]defgetMessageParameters(self)->Optional[Dict[str,str]]:""" Returns a message parameters as a dictionary. .. versionadded:: 3.4.0 See Also -------- :meth:`PySparkException.getErrorClass` :meth:`PySparkException.getMessage` :meth:`PySparkException.getQueryContext` :meth:`PySparkException.getSqlState` """returnself._messageParameters
[docs]defgetSqlState(self)->Optional[str]:""" Returns an SQLSTATE as a string. Errors generated in Python have no SQLSTATE, so it always returns None. .. versionadded:: 3.4.0 See Also -------- :meth:`PySparkException.getErrorClass` :meth:`PySparkException.getMessage` :meth:`PySparkException.getMessageParameters` :meth:`PySparkException.getQueryContext` """returnNone
[docs]defgetMessage(self)->str:""" Returns full error message. .. versionadded:: 4.0.0 See Also -------- :meth:`PySparkException.getErrorClass` :meth:`PySparkException.getMessageParameters` :meth:`PySparkException.getQueryContext` :meth:`PySparkException.getSqlState` """returnf"[{self.getErrorClass()}] {self._message}"
[docs]defgetQueryContext(self)->List["QueryContext"]:""" Returns :class:`QueryContext`. .. versionadded:: 4.0.0 See Also -------- :meth:`PySparkException.getErrorClass` :meth:`PySparkException.getMessageParameters` :meth:`PySparkException.getMessage` :meth:`PySparkException.getSqlState` """returnself._contexts
[docs]classAnalysisException(PySparkException):""" Failed to analyze a SQL query plan. """
[docs]classSessionNotSameException(PySparkException):""" Performed the same operation on different SparkSession. """
[docs]classTempTableAlreadyExistsException(AnalysisException):""" Failed to create temp view since it is already exists. """
[docs]classParseException(AnalysisException):""" Failed to parse a SQL command. """
[docs]classIllegalArgumentException(PySparkException):""" Passed an illegal or inappropriate argument. """
[docs]classArithmeticException(PySparkException):""" Arithmetic exception thrown from Spark with an error class. """
[docs]classUnsupportedOperationException(PySparkException):""" Unsupported operation exception thrown from Spark with an error class. """
[docs]classArrayIndexOutOfBoundsException(PySparkException):""" Array index out of bounds exception thrown from Spark with an error class. """
[docs]classDateTimeException(PySparkException):""" Datetime exception thrown from Spark with an error class. """
[docs]classNumberFormatException(IllegalArgumentException):""" Number format exception thrown from Spark with an error class. """
[docs]classStreamingQueryException(PySparkException):""" Exception that stopped a :class:`StreamingQuery`. """
[docs]classQueryExecutionException(PySparkException):""" Failed to execute a query. """
[docs]classPythonException(PySparkException):""" Exceptions thrown from Python workers. """
[docs]classSparkRuntimeException(PySparkException):""" Runtime exception thrown from Spark with an error class. """
[docs]classSparkUpgradeException(PySparkException):""" Exception thrown because of Spark upgrade. """
[docs]classSparkNoSuchElementException(PySparkException):""" Exception thrown for `java.util.NoSuchElementException`. """
[docs]classUnknownException(PySparkException):""" None of the other exceptions. """
[docs]classPySparkValueError(PySparkException,ValueError):""" Wrapper class for ValueError to support error classes. """
[docs]classPySparkTypeError(PySparkException,TypeError):""" Wrapper class for TypeError to support error classes. """
[docs]classPySparkIndexError(PySparkException,IndexError):""" Wrapper class for IndexError to support error classes. """
[docs]classPySparkAttributeError(PySparkException,AttributeError):""" Wrapper class for AttributeError to support error classes. """
[docs]classPySparkRuntimeError(PySparkException,RuntimeError):""" Wrapper class for RuntimeError to support error classes. """
[docs]classPySparkAssertionError(PySparkException,AssertionError):""" Wrapper class for AssertionError to support error classes. """def__init__(self,message:Optional[str]=None,errorClass:Optional[str]=None,messageParameters:Optional[Dict[str,str]]=None,data:Optional[Iterable["Row"]]=None,):super().__init__(message,errorClass,messageParameters)self.data=data
[docs]classPySparkNotImplementedError(PySparkException,NotImplementedError):""" Wrapper class for NotImplementedError to support error classes. """
[docs]classPySparkPicklingError(PySparkException,PicklingError):""" Wrapper class for pickle.PicklingError to support error classes. """
[docs]classRetriesExceeded(PySparkException):""" Represents an exception which is considered retriable, but retry limits were exceeded """
[docs]classPySparkKeyError(PySparkException,KeyError):""" Wrapper class for KeyError to support error classes. """
[docs]classPySparkImportError(PySparkException,ImportError):""" Wrapper class for ImportError to support error classes. """
[docs]classQueryContextType(Enum):""" The type of :class:`QueryContext`. .. versionadded:: 4.0.0 """SQL=0DataFrame=1
[docs]classQueryContext(ABC):""" Query context of a :class:`PySparkException`. It helps users understand where error occur while executing queries. .. versionadded:: 4.0.0 """@abstractmethoddefcontextType(self)->QueryContextType:""" The type of this query context. """...@abstractmethoddefobjectType(self)->str:""" The object type of the query which throws the exception. If the exception is directly from the main query, it should be an empty string. Otherwise, it should be the exact object type in upper case. For example, a "VIEW". """...@abstractmethoddefobjectName(self)->str:""" The object name of the query which throws the exception. If the exception is directly from the main query, it should be an empty string. Otherwise, it should be the object name. For example, a view name "V1". """...@abstractmethoddefstartIndex(self)->int:""" The starting index in the query text which throws the exception. The index starts from 0. """...@abstractmethoddefstopIndex(self)->int:""" The stopping index in the query which throws the exception. The index starts from 0. """...@abstractmethoddeffragment(self)->str:""" The corresponding fragment of the query which throws the exception. """...@abstractmethoddefcallSite(self)->str:""" The user code (call site of the API) that caused throwing the exception. """...@abstractmethoddefsummary(self)->str:""" Summary of the exception cause. """...