Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions Sources/SCInject/Container.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public final class DefaultContainer: Container {

public func resolve<T>(_ 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
Expand All @@ -84,7 +85,8 @@ public final class DefaultContainer: Container {

public func resolve<T>(_ 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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions Sources/SCInject/Exception.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}