Skip to content

Commit e6adf37

Browse files
committed
feat: Arguments & Graph Instances
1 parent 204655e commit e6adf37

27 files changed

+1681
-1055
lines changed

.swiftlint.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
disabled_rules: # rules to exclude from running
22
- trailing_whitespace
3+
- file_length
34

45
opt_in_rules: # some rules are disabled by default, so you need to opt-in
56
- redundant_void_return
@@ -15,4 +16,4 @@ excluded: # paths to ignore during linting. Takes precedence over `included`.
1516
- .build
1617
- Docs
1718

18-
line_length: 120 # max line length
19+
line_length: 120 # max line length

Sources/AstrojectCore/AstrojectError.swift

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// AstrojectError.swift
3-
// Astroject
4-
//
5-
// Created by Porter McGary on 2/27/25.
6-
//
7-
81
import Foundation
92

103
/// Represents errors that can occur during dependency registration and resolution
@@ -29,25 +22,43 @@ public enum AstrojectError: LocalizedError {
2922
/// A circular dependency is detected during the resolution process.
3023
///
3124
/// This case indicates that a chain of dependencies forms a loop, preventing successful resolution.
32-
case circularDependencyDetected
25+
case circularDependencyDetected(type: String, name: String? = nil)
3326

3427
/// An error occurred within the factory closure during dependency resolution.
3528
///
3629
/// This case wraps an underlying error that occurred while executing the factory
3730
/// closure responsible for creating a dependency instance.
3831
case underlyingError(Error)
32+
33+
/// An invalid instance was encountered during resolution.
34+
///
35+
/// This error occurs when the instance being resolved is not valid or
36+
/// does not conform to the expected type. This can happen if the
37+
/// Instance implementation is incorrect, or if there is a mismatch
38+
/// between the registered type and the actual type of the resolved instance.
39+
case invalidInstance
3940

4041
/// Provides a user-friendly description of the error.
4142
public var errorDescription: String? {
4243
switch self {
43-
case .alreadyRegistered:
44-
return "A registration with the same ProductKey already exists."
44+
case .alreadyRegistered(let type, let name):
45+
if let name = name {
46+
return "A registration for type '\(type)' with name '\(name)' already exists."
47+
} else {
48+
return "A registration for type '\(type)' already exists."
49+
}
4550
case .noRegistrationFound:
4651
return "No registration found for the requested dependency."
47-
case .circularDependencyDetected:
48-
return "A circular dependency was detected."
52+
case .circularDependencyDetected(let type, let name):
53+
if let name = name {
54+
return "A circular dependency was detected while resolving type '\(type)' with name '\(name)'."
55+
} else {
56+
return "A circular dependency was detected."
57+
}
4958
case .underlyingError(let error):
5059
return "An error occurred within the factory closure: \(error.localizedDescription)"
60+
case .invalidInstance:
61+
return "The resolved instance is invalid or of an unexpected type."
5162
}
5263
}
5364

@@ -62,6 +73,8 @@ public enum AstrojectError: LocalizedError {
6273
return "Review your dependency graph to eliminate circular dependencies."
6374
case .underlyingError:
6475
return "Inspect the underlying error for more details."
76+
case .invalidInstance:
77+
return "The resolved instance did not match the expected type or was invalid."
6578
}
6679
}
6780

@@ -77,6 +90,8 @@ public enum AstrojectError: LocalizedError {
7790
return "Break the circular dependency by introducing an abstraction or using a different dependency injection pattern or by using `postInitAction` to initialize cyclical dependencies."
7891
case .underlyingError:
7992
return "Check the factory closure for errors and ensure that it's correctly implemented."
93+
case .invalidInstance:
94+
return "Ensure that the Instance implementation is correct and that the registered type matches the actual type of the resolved instance."
8095
}
8196
}
8297
}
@@ -92,12 +107,14 @@ extension AstrojectError: Equatable {
92107
/// - Returns: `true` if the errors are equal, `false` otherwise.
93108
public static func == (lhs: AstrojectError, rhs: AstrojectError) -> Bool {
94109
switch (lhs, rhs) {
95-
case (.alreadyRegistered(let lhsType, let lhsName), .alreadyRegistered(let rhsType, let rhsName)):
96-
// Compare the associated types and names for alreadyRegistered errors.
110+
case (.alreadyRegistered(let lhsType, let lhsName), .alreadyRegistered(let rhsType, let rhsName)),
111+
(.circularDependencyDetected(let lhsType, let lhsName),
112+
.circularDependencyDetected(let rhsType, let rhsName)):
113+
// Compare the associated types and names for alreadyRegistered and circularDependencyDetected errors.
97114
return lhsType == rhsType && lhsName == rhsName
98115
case (.noRegistrationFound, .noRegistrationFound),
99-
(.circularDependencyDetected, .circularDependencyDetected):
100-
// noRegistrationFound and circularDependencyDetected errors are equal if they are the same case.
116+
(.invalidInstance, .invalidInstance):
117+
// noRegistrationFound and invalidInstance errors are equal if they are the same case.
101118
return true
102119
case (.underlyingError(let lhsError), .underlyingError(let rhsError)):
103120
// Compare the descriptions of the underlying errors.

0 commit comments

Comments
 (0)