From 2851c5083f883670fcdb6485fd3b9a428b344a76 Mon Sep 17 00:00:00 2001 From: profitroll Date: Fri, 23 Sep 2022 13:53:33 +0200 Subject: [PATCH] Errors added --- pyrmv/__init__.py | 4 +- pyrmv/errors/api_errors.py | 55 ++++++++++++ pyrmv/errors/int_errors.py | 29 +++++++ pyrmv/errors/ps_errors.py | 13 +++ pyrmv/errors/sot_errors.py | 49 +++++++++++ pyrmv/errors/svc_errors.py | 145 ++++++++++++++++++++++++++++++++ pyrmv/errors/unknown_error.py | 15 ++++ pyrmv/utility/__init__.py | 0 pyrmv/utility/find_exception.py | 102 ++++++++++++++++++++++ 9 files changed, 410 insertions(+), 2 deletions(-) create mode 100644 pyrmv/errors/api_errors.py create mode 100644 pyrmv/errors/int_errors.py create mode 100644 pyrmv/errors/ps_errors.py create mode 100644 pyrmv/errors/sot_errors.py create mode 100644 pyrmv/errors/svc_errors.py create mode 100644 pyrmv/errors/unknown_error.py create mode 100644 pyrmv/utility/__init__.py create mode 100644 pyrmv/utility/find_exception.py diff --git a/pyrmv/__init__.py b/pyrmv/__init__.py index f823ee7..56ccece 100644 --- a/pyrmv/__init__.py +++ b/pyrmv/__init__.py @@ -36,5 +36,5 @@ __license__ = "MIT License" __author__ = "Profitroll" from . import raw -from .methods import * -from .errors import * \ No newline at end of file +from . import errors +from .methods import * \ No newline at end of file diff --git a/pyrmv/errors/api_errors.py b/pyrmv/errors/api_errors.py new file mode 100644 index 0000000..8119d16 --- /dev/null +++ b/pyrmv/errors/api_errors.py @@ -0,0 +1,55 @@ +class ApiAuthError(Exception): + """ + Access denied for accessId provided. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class ApiQuotaError(Exception): + """ + Quota exceeded for accessId provided. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class ApiTooManyRequests(Exception): + """ + Too many requests. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class ApiParamError(Exception): + """Exception raised for errors in the input arguments. + + ### Attributes: + * errorCode: Client error code from HAFAS ReST Request Errors. + * errorText: Description of an error occurred. + """ + + def __init__(self, errorCode: str, errorText: str): + self.errorCode = errorCode + self.errorText = errorText + super().__init__(self.errorText) + + def __str__(self): + return f'{self.errorCode} -> {self.errorText}' + +class ApiFormatError(Exception): + """ + Response format not supported. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ \ No newline at end of file diff --git a/pyrmv/errors/int_errors.py b/pyrmv/errors/int_errors.py new file mode 100644 index 0000000..b92dde8 --- /dev/null +++ b/pyrmv/errors/int_errors.py @@ -0,0 +1,29 @@ +class IntError(Exception): + """ + Internal RMV server error. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class IntGatewayError(Exception): + """ + Communication error with RMV backend systems. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class IntTimeoutError(Exception): + """ + Timeout during service processing. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ \ No newline at end of file diff --git a/pyrmv/errors/ps_errors.py b/pyrmv/errors/ps_errors.py new file mode 100644 index 0000000..83ac32c --- /dev/null +++ b/pyrmv/errors/ps_errors.py @@ -0,0 +1,13 @@ +class PsIncorrectParamError(Exception): + """ + An invalid parameter combination was requested, i.e. the + defined range of stable segments encompassed all public + transport sections or it was attempted to search + forward/backward from the end/beginning of the + connection. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ \ No newline at end of file diff --git a/pyrmv/errors/sot_errors.py b/pyrmv/errors/sot_errors.py new file mode 100644 index 0000000..6b2459f --- /dev/null +++ b/pyrmv/errors/sot_errors.py @@ -0,0 +1,49 @@ +class SotAlreadyArrivedError(Exception): + """ + Trip already arrived. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SotNotStartedError(Exception): + """ + Trip not started. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SotCancelledError(Exception): + """ + Trip cancelled. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SotAllTrainsFilteredError(Exception): + """ + All trips filtered. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SotStayOnTripError(Exception): + """ + No change. Stay on trip. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ \ No newline at end of file diff --git a/pyrmv/errors/svc_errors.py b/pyrmv/errors/svc_errors.py new file mode 100644 index 0000000..c2073ad --- /dev/null +++ b/pyrmv/errors/svc_errors.py @@ -0,0 +1,145 @@ +class SvcParamError(Exception): + """Exception raised for errors in the input arguments. + + ### Attributes: + * errorCode: Client error code from HAFAS ReST Request Errors. + * errorText: Description of an error occurred. + """ + + def __init__(self, errorCode: str, errorText: str): + self.errorCode = errorCode + self.errorText = errorText + super().__init__(self.errorText) + + def __str__(self): + return f'{self.errorCode} -> {self.errorText}' + +class SvcLocationError(Exception): + """ + Location missing or invalid. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SvcLocationArrivalError(Exception): + """ + Arrival location missing or invalid. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SvcLocationDepartureError(Exception): + """ + Departure location missing or invalid. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SvcLocationViaError(Exception): + """ + Unknown change stop. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SvcLocationEqualError(Exception): + """ + Origin/destination or vias are equal. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SvcLocationNearError(Exception): + """ + Origin and destination are too close. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SvcDatetimeError(Exception): + """ + Date/time are missing or invalid. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SvcDatetimePeriodError(Exception): + """ + Date/time are not in timetable or allowed period. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SvcProductError(Exception): + """ + Product field missing or invalid. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SvcContextError(Exception): + """ + Context is invalid. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SvcNoResultError(Exception): + """ + No result found. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SvcSearchError(Exception): + """ + Unsuccessful search. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ + +class SvcNoMatchError(Exception): + """ + No match found. + """ + def __init__(self): + super().__init__(self.__doc__) + + def __str__(self): + return self.__doc__ \ No newline at end of file diff --git a/pyrmv/errors/unknown_error.py b/pyrmv/errors/unknown_error.py new file mode 100644 index 0000000..0f56ca6 --- /dev/null +++ b/pyrmv/errors/unknown_error.py @@ -0,0 +1,15 @@ +class UnknownError(Exception): + """Exception raised but error is not known. + + ### Attributes: + * errorCode: Client error code from HAFAS ReST Request Errors. + * errorText: Description of an error occurred. + """ + + def __init__(self, errorCode: str, errorText: str): + self.errorCode = errorCode + self.errorText = errorText + super().__init__(self.errorText) + + def __str__(self): + return f'{self.errorCode} -> {self.errorText}' \ No newline at end of file diff --git a/pyrmv/utility/__init__.py b/pyrmv/utility/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pyrmv/utility/find_exception.py b/pyrmv/utility/find_exception.py new file mode 100644 index 0000000..066ed8d --- /dev/null +++ b/pyrmv/utility/find_exception.py @@ -0,0 +1,102 @@ +from pyrmv.errors.api_errors import ApiAuthError, ApiFormatError, ApiParamError, ApiQuotaError, ApiTooManyRequests +from pyrmv.errors.int_errors import IntError, IntGatewayError, IntTimeoutError +from pyrmv.errors.ps_errors import PsIncorrectParamError +from pyrmv.errors.sot_errors import SotAllTrainsFilteredError, SotAlreadyArrivedError, SotCancelledError, SotNotStartedError, SotStayOnTripError +from pyrmv.errors.svc_errors import SvcContextError, SvcDatetimeError, SvcDatetimePeriodError, SvcLocationArrivalError, SvcLocationDepartureError, SvcLocationEqualError, SvcLocationError, SvcLocationNearError, SvcLocationViaError, SvcNoMatchError, SvcNoResultError, SvcProductError, SvcSearchError +from pyrmv.errors.unknown_error import UnknownError + +def find_exception(data: dict): + """Scan returned dict for errorCode from RMV. + Raises different exceptions if errorCode is not None. + + ### Args: + * data (dict): Response from RMV as a dict. + + ### Raises: + * Any: Formatted as "errorCode -> errorText" if ApiParamError and UnknownError or as a single massage for others. + """ + if "errorCode" in data: + + if data["errorCode"] == "API_AUTH": + raise ApiAuthError() + + elif data["errorCode"] == "API_QUOTA": + raise ApiQuotaError() + + elif data["errorCode"] == "API_TOO_MANY_REQUESTS": + raise ApiTooManyRequests() + + elif data["errorCode"] == "API_PARAM": + raise ApiParamError(errorCode=data["errorCode"], errorText=data["errorText"]) + + elif data["errorCode"] == "API_FORMAT": + raise ApiFormatError() + + elif data["errorCode"] == "SVC_LOC": + raise SvcLocationError() + + elif data["errorCode"] == "SVC_LOC_ARR": + raise SvcLocationArrivalError() + + elif data["errorCode"] == "SVC_LOC_DEP": + raise SvcLocationDepartureError() + + elif data["errorCode"] == "SVC_LOC_VIA": + raise SvcLocationViaError() + + elif data["errorCode"] == "SVC_LOC_EQUAL": + raise SvcLocationEqualError() + + elif data["errorCode"] == "SVC_LOC_NEAR": + raise SvcLocationNearError() + + elif data["errorCode"] == "SVC_DATATIME": + raise SvcDatetimeError() + + elif data["errorCode"] == "SVC_DATATIME_PERIOD": + raise SvcDatetimePeriodError() + + elif data["errorCode"] == "SVC_PROD": + raise SvcProductError() + + elif data["errorCode"] == "SVC_CTX": + raise SvcContextError() + + elif data["errorCode"] == "SVC_NO_RESULT": + raise SvcNoResultError() + + elif data["errorCode"] == "SVC_FAILED_SEARCH": + raise SvcSearchError() + + elif data["errorCode"] == "SVC_NO_MATCH": + raise SvcNoMatchError() + + elif data["errorCode"] == "INT_ERR": + raise IntError() + + elif data["errorCode"] == "INT_GATEWAY": + raise IntGatewayError() + + elif data["errorCode"] == "INT_TIMEOUT": + raise IntTimeoutError() + + elif data["errorCode"] == "SOT_AT_DEST": + raise SotAlreadyArrivedError() + + elif data["errorCode"] == "SOT_BEFORE_START": + raise SotNotStartedError() + + elif data["errorCode"] == "SOT_CANCELLED": + raise SotCancelledError() + + elif data["errorCode"] == "SOT_ALL_TRAINS_FILTERED": + raise SotAllTrainsFilteredError() + + elif data["errorCode"] == "SOT_STAY_IN_CURRENT_CONNECTION": + raise SotStayOnTripError() + + elif data["errorCode"] == "PARTIALSEARCH_INCORRECT_PARAM": + raise PsIncorrectParamError() + + else: + raise UnknownError(errorCode=data["errorCode"], errorText=data["errorText"]) \ No newline at end of file