Skip to main content

exceptions

The code in the exceptions.py defines a set of custom exception classes for error handling in Python, specifically for errors that might occur while interacting with the YetiForce API.

1. Base Class: YetiForceError

class YetiForceError(Exception):
pass
  • Purpose: YetiForceError is a custom base exception class that inherits from Python's built-in Exception class.
  • Usage: This class serves as the root exception for all custom exceptions related to YetiForce errors.

2. Subclass: AuthenticationError

class AuthenticationError(YetiForceError):
pass
  • Purpose: AuthenticationError is a subclass of YetiForceError and represents an error specifically related to authentication failures.
  • Usage: This exception would be raised when an authentication-related issue occurs, such as invalid credentials or token failures.

3. Subclass: APIError

class APIError(YetiForceError):
pass
  • Purpose: APIError is another subclass of YetiForceError, but it represents a more general error that can occur while interacting with the YetiForce API.
  • Usage: This exception would be raised for errors that happen during API calls, such as server errors, invalid responses, or other issues when interacting with the API.

4. Subclass: TokenError

class TokenError(APIError):
pass
  • Purpose: TokenError is a more specific subclass of APIError that indicates issues related to tokens (such as expired or invalid tokens) while making API calls.
  • Usage: This exception is specifically designed to handle errors related to authentication tokens or session tokens, which are common in API interactions.

Purpose of These Custom Exceptions:

  • More granular and specific error handling. Instead of catching generic exceptions, the code can catch and handle specific error types like AuthenticationError or TokenError to take appropriate action (e.g., re-authentication or requesting a new token).

  • By defining specific exception classes, it is possible to provide more useful and accurate error messages when something goes wrong. For instance, if a token has expired, a TokenError can be raised, making it clear that the issue is related to the token rather than a generic API failure.

Example Usage:

Here’s how you might use these custom exceptions in your code:

try:
# Some code that may raise an error
raise AuthenticationError("Invalid credentials provided.")
except AuthenticationError as e:
print(f"Authentication failed: {e}")
except APIError as e:
print(f"API error: {e}")
except YetiForceError as e:
print(f"YetiForce error: {e}")

Explanation:

  • If an AuthenticationError occurs, the message "Authentication failed: Invalid credentials provided." will be printed.
  • If a general APIError occurs (but not specifically an AuthenticationError), it will be caught by the APIError block.
  • If any other YetiForceError occurs, the general exception block for YetiForceError will catch it.

Hierarchy:

  • YetiForceError: The base exception for all YetiForce-related errors.
    • AuthenticationError: A specific error related to authentication issues.
    • APIError: A more general error related to the API.
      • TokenError: A more specific error related to issues with tokens (e.g., expired or invalid tokens).

In conclusion, this code is useful for structured error handling, allowing you to catch and handle different types of errors related to the YetiForce platform or API in a more precise and controlled manner.