diff --git a/Sources/SCInject/Container.swift b/Sources/SCInject/Container.swift index a016ffe..303cb23 100644 --- a/Sources/SCInject/Container.swift +++ b/Sources/SCInject/Container.swift @@ -72,7 +72,8 @@ public final class DefaultContainer: Container { public func resolve(_ type: T.Type) -> T { guard let instance = tryResolve(type) else { - let message = errorMessage("Failed to resolve given type -- TYPE=\(type)") + let message = "Failed to resolve given type -- TYPE=\(type)" + Exception.raise(reason: message) fatalError(message) } return instance @@ -84,7 +85,8 @@ public final class DefaultContainer: Container { public func resolve(_ type: T.Type, name: RegistrationName) -> T { guard let instance = tryResolve(type, name: name) else { - let message = errorMessage("Failed to resolve given type -- TYPE=\(type) NAME=\(name.rawValue)") + let message = "Failed to resolve given type -- TYPE=\(type) NAME=\(name.rawValue)" + Exception.raise(reason: message) fatalError(message) } return instance @@ -115,8 +117,8 @@ public final class DefaultContainer: Container { lock.lock(); defer { lock.unlock() } let identifier = identifier(of: type, name: name) if resolvers[identifier] != nil { - let message = - errorMessage("Given type is already registered -- TYPE=\(type) NAME=\(name?.rawValue ?? "nil")") + let message = "Given type is already registered -- TYPE=\(type) NAME=\(name?.rawValue ?? "nil")" + Exception.raise(reason: message) fatalError(message) } resolvers[identifier] = makeResolver(scope ?? defaultScope, closure: closure) @@ -155,10 +157,6 @@ public final class DefaultContainer: Container { let typeIdentifier: ObjectIdentifier let description: String } - - private func errorMessage(_ message: String) -> String { - "SCInjectError - \(message)" - } } private protocol ReferenceResolver { diff --git a/Sources/SCInject/Exception.swift b/Sources/SCInject/Exception.swift new file mode 100644 index 0000000..0f6ac62 --- /dev/null +++ b/Sources/SCInject/Exception.swift @@ -0,0 +1,27 @@ +// +// Copyright 2024 Marcin Iwanicki and contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import Foundation + +final class Exception: NSException { + static func raise(reason: String, userInfo: [String: String] = [:]) { + Exception( + name: NSExceptionName(rawValue: "SCInject.Exception"), + reason: reason, + userInfo: userInfo + ).raise() + } +}