|
| 1 | +// |
| 2 | +// AstrojectError.swift |
| 3 | +// Astroject |
| 4 | +// |
| 5 | +// Created by Porter McGary on 2/27/25. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +/// Represents errors that can occur during dependency registration and resolution |
| 11 | +/// within the Astroject dependency injection framework. |
| 12 | +/// |
| 13 | +/// This enum consolidates various error scenarios, including issues related to |
| 14 | +/// dependency registration, resolution, and factory execution. |
| 15 | +/// It conforms to `LocalizedError` to provide user-friendly error descriptions, |
| 16 | +/// failure reasons, and recovery suggestions. |
| 17 | +public enum AstrojectError: LocalizedError { |
| 18 | + /// A registration is attempted with a `RegistrationKey` that already exists. |
| 19 | + /// |
| 20 | + /// This case indicates that a dependency registration with the same type and |
| 21 | + /// optional name has already been performed. |
| 22 | + case alreadyRegistered(type: String, name: String? = nil) |
| 23 | + |
| 24 | + /// A dependency is requested but no corresponding registration is found in the container. |
| 25 | + /// |
| 26 | + /// This case occurs when attempting to resolve a dependency that has not been registered. |
| 27 | + case noRegistrationFound |
| 28 | + |
| 29 | + /// A circular dependency is detected during the resolution process. |
| 30 | + /// |
| 31 | + /// This case indicates that a chain of dependencies forms a loop, preventing successful resolution. |
| 32 | + case circularDependencyDetected |
| 33 | + |
| 34 | + /// An error occurred within the factory closure during dependency resolution. |
| 35 | + /// |
| 36 | + /// This case wraps an underlying error that occurred while executing the factory |
| 37 | + /// closure responsible for creating a dependency instance. |
| 38 | + case underlyingError(Error) |
| 39 | + |
| 40 | + /// Provides a user-friendly description of the error. |
| 41 | + public var errorDescription: String? { |
| 42 | + switch self { |
| 43 | + case .alreadyRegistered: |
| 44 | + return "A registration with the same ProductKey already exists." |
| 45 | + case .noRegistrationFound: |
| 46 | + return "No registration found for the requested dependency." |
| 47 | + case .circularDependencyDetected: |
| 48 | + return "A circular dependency was detected." |
| 49 | + case .underlyingError(let error): |
| 50 | + return "An error occurred within the factory closure: \(error.localizedDescription)" |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// Provides a reason for the error, explaining why it occurred. |
| 55 | + public var failureReason: String? { |
| 56 | + switch self { |
| 57 | + case .alreadyRegistered: |
| 58 | + return "Attempting to register a dependency with a ProductKey that has already been used." |
| 59 | + case .noRegistrationFound: |
| 60 | + return "Register the dependency before attempting to resolve it." |
| 61 | + case .circularDependencyDetected: |
| 62 | + return "Review your dependency graph to eliminate circular dependencies." |
| 63 | + case .underlyingError: |
| 64 | + return "Inspect the underlying error for more details." |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /// Provides a suggestion for recovering from the error. |
| 69 | + public var recoverySuggestion: String? { |
| 70 | + switch self { |
| 71 | + case .alreadyRegistered: |
| 72 | + return "Use a different ProductKey or remove the existing registration before registering a new one." |
| 73 | + case .noRegistrationFound: |
| 74 | + return "Use the `register` or `registerAsync` method to register the dependency." |
| 75 | + case .circularDependencyDetected: |
| 76 | + // swiftlint:disable:next line_length |
| 77 | + return "Break the circular dependency by introducing an abstraction or using a different dependency injection pattern or by using `postInitAction` to initialize cyclical dependencies." |
| 78 | + case .underlyingError: |
| 79 | + return "Check the factory closure for errors and ensure that it's correctly implemented." |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// Extends `AstrojectError` to conform to `Equatable` when the associated `Product` type is also `Equatable`. |
| 85 | +/// |
| 86 | +/// This extension allows for easy comparison of `AstrojectError` instances based on their associated values. |
| 87 | +extension AstrojectError: Equatable { |
| 88 | + /// Checks if two `AstrojectError` instances are equal. |
| 89 | + /// |
| 90 | + /// - Parameters: |
| 91 | + /// - lhs: The left-hand side `AstrojectError` instance. |
| 92 | + /// - rhs: The right-hand side `AstrojectError` instance. |
| 93 | + /// - Returns: `true` if the errors are equal, `false` otherwise. |
| 94 | + public static func == (lhs: AstrojectError, rhs: AstrojectError) -> Bool { |
| 95 | + switch (lhs, rhs) { |
| 96 | + case (.alreadyRegistered(let lhsType, let lhsName), .alreadyRegistered(let rhsType, let rhsName)): |
| 97 | + // Compare the associated types and names for alreadyRegistered errors. |
| 98 | + return lhsType == rhsType && lhsName == rhsName |
| 99 | + case (.noRegistrationFound, .noRegistrationFound), |
| 100 | + (.circularDependencyDetected, .circularDependencyDetected): |
| 101 | + // noRegistrationFound and circularDependencyDetected errors are equal if they are the same case. |
| 102 | + return true |
| 103 | + case (.underlyingError(let lhsError), .underlyingError(let rhsError)): |
| 104 | + // Compare the descriptions of the underlying errors. |
| 105 | + return String(describing: lhsError) == String(describing: rhsError) |
| 106 | + default: |
| 107 | + // All other cases are not equal. |
| 108 | + return false |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments