Skip to content

Commit 527bf0a

Browse files
Throw exception when the resolution fails (#15)
Test Plan: - Ensure all CI checks pass
1 parent e671d21 commit 527bf0a

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

Sources/SCInject/Container.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public final class DefaultContainer: Container {
7272

7373
public func resolve<T>(_ type: T.Type) -> T {
7474
guard let instance = tryResolve(type) else {
75-
let message = errorMessage("Failed to resolve given type -- TYPE=\(type)")
75+
let message = "Failed to resolve given type -- TYPE=\(type)"
76+
Exception.raise(reason: message)
7677
fatalError(message)
7778
}
7879
return instance
@@ -84,7 +85,8 @@ public final class DefaultContainer: Container {
8485

8586
public func resolve<T>(_ type: T.Type, name: RegistrationName) -> T {
8687
guard let instance = tryResolve(type, name: name) else {
87-
let message = errorMessage("Failed to resolve given type -- TYPE=\(type) NAME=\(name.rawValue)")
88+
let message = "Failed to resolve given type -- TYPE=\(type) NAME=\(name.rawValue)"
89+
Exception.raise(reason: message)
8890
fatalError(message)
8991
}
9092
return instance
@@ -115,8 +117,8 @@ public final class DefaultContainer: Container {
115117
lock.lock(); defer { lock.unlock() }
116118
let identifier = identifier(of: type, name: name)
117119
if resolvers[identifier] != nil {
118-
let message =
119-
errorMessage("Given type is already registered -- TYPE=\(type) NAME=\(name?.rawValue ?? "nil")")
120+
let message = "Given type is already registered -- TYPE=\(type) NAME=\(name?.rawValue ?? "nil")"
121+
Exception.raise(reason: message)
120122
fatalError(message)
121123
}
122124
resolvers[identifier] = makeResolver(scope ?? defaultScope, closure: closure)
@@ -155,10 +157,6 @@ public final class DefaultContainer: Container {
155157
let typeIdentifier: ObjectIdentifier
156158
let description: String
157159
}
158-
159-
private func errorMessage(_ message: String) -> String {
160-
"SCInjectError - \(message)"
161-
}
162160
}
163161

164162
private protocol ReferenceResolver {

Sources/SCInject/Exception.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Copyright 2024 Marcin Iwanicki and contributors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
import Foundation
18+
19+
final class Exception: NSException {
20+
static func raise(reason: String, userInfo: [String: String] = [:]) {
21+
Exception(
22+
name: NSExceptionName(rawValue: "SCInject.Exception"),
23+
reason: reason,
24+
userInfo: userInfo
25+
).raise()
26+
}
27+
}

0 commit comments

Comments
 (0)