Package 

Class BorgException

  • All Implemented Interfaces:
    java.io.Serializable

    
    public class BorgException
    extends Exception
                        

    A hierarchy of exceptions that can occur during component initialization.

    Why sealed class?

    • Provides exhaustive when expressions for error handling

    • Ensures all possible failures are documented

    • Prevents creation of unexpected error types

    • Enables smart casting in error handlers

    Design principles:

    • Clear Error Categories: Each subclass represents a distinct failure mode

    • Rich Context: Each exception carries relevant data for debugging

    • Helpful Messages: Error descriptions guide users to solutions

    • Clean Stack Traces: Original causes are preserved when wrapping errors

    Example usage:

    try {
        borg.assimilate(context)
    } catch (e: BorgException) {
        when (e) {
            is CircularDependencyException -> {
                // Handle circular dependency detected
                Log.e("Borg", "Circular dependency in: ${e.cycle}")
            }
            is DroneNotFoundException -> {
                // Handle missing dependency
                Log.e("Borg", "Missing drone: ${e.requiredDrone}")
            }
            is AssimilationException -> {
                // Handle initialization failure
                Log.e("Borg", "Failed to initialize: ${e.drone}", e.cause)
            }
            is DroneNotAssimilatedException -> {
                // Handle attempt to access uninitialized drone
                Log.e("Borg", "Drone not ready: ${e.drone}")
            }
        }
    }
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      public final class BorgException.CircularDependencyException

      Thrown when components form a dependency cycle.

      Why track the full cycle?

      • Helps developers visualize the dependency problem

      • Makes it clear which components need restructuring

      • Provides context for architectural decisions

      Example cycles:

      • DatabaseDrone -> RepositoryDrone -> ServiceDrone -> DatabaseDrone

      • ConfigDrone -> NetworkDrone -> ApiDrone -> ConfigDrone

      Common fixes:

      • Extract shared dependencies into a separate drone

      • Use dependency injection to break cycles

      • Refactor component responsibilities

      • Use lazy initialization where appropriate

      public final class BorgException.AssimilationException

      Thrown when a component fails to initialize.

      Why wrap the original exception?

      • Preserves the original error context

      • Adds initialization-specific context

      • Maintains exception hierarchy

      • Enables detailed error reporting

      Common causes:

      • Configuration errors

      • Resource access failures

      • Network connectivity issues

      • Invalid component state

      public final class BorgException.DroneNotFoundException

      Thrown when a required component is missing from the collective.

      Why check dependencies upfront?

      • Fails fast before any initialization starts

      • Prevents partial system initialization

      • Makes dependency errors obvious

      • Helps maintain clean architecture

      Common scenarios:

      • Missing dependency registration

      • Incorrect dependency declaration

      • Module configuration errors

      Best practices:

      • Use dependency injection frameworks

      • Maintain a central registry of drones

      • Document dependencies clearly

      • Write integration tests

      public final class BorgException.DroneNotAssimilatedException

      Thrown when attempting to access a drone's result before it has been initialized.

      Common scenarios:

      • Accessing drone outside of assimilation process

      • Race condition in parallel initialization

      • Missing dependency declaration

      • Incorrect initialization order

      Best practices:

      • Always declare dependencies in requiredDrones()

      • Use requireAssimilated() only when certain of initialization

      • Handle potential DroneNotAssimilatedException in async code

      • Consider lazy initialization for optional dependencies

    • Method Summary

      Modifier and Type Method Description
      Throwable getCause()
      String getMessage()
      • Methods inherited from class java.lang.Exception

        addSuppressed, fillInStackTrace, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait