From 1e9fae949208be18b0d8b0c1837fc518bd7dc5e6 Mon Sep 17 00:00:00 2001 From: Marcin Iwanicki Date: Sun, 8 Sep 2024 19:40:08 +0100 Subject: [PATCH] Ensure the name and type are part of the reason --- Sources/SCInject/ContainerError.swift | 2 +- Sources/SCInjectObjc/ContainerException.m | 13 ++++++++++--- Tests/SCInject/ContainerTests.swift | 6 +++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Sources/SCInject/ContainerError.swift b/Sources/SCInject/ContainerError.swift index 2f075f3..b3d77e4 100644 --- a/Sources/SCInject/ContainerError.swift +++ b/Sources/SCInject/ContainerError.swift @@ -40,7 +40,7 @@ public struct ContainerError: Error { } static func raise(reason: String, type: String, name: String?) { - ContainerException.raise(reason: reason, type: type, name: nil) + ContainerException.raise(reason: reason, type: type, name: name) } // MARK: - Private diff --git a/Sources/SCInjectObjc/ContainerException.m b/Sources/SCInjectObjc/ContainerException.m index af97b5c..515c073 100644 --- a/Sources/SCInjectObjc/ContainerException.m +++ b/Sources/SCInjectObjc/ContainerException.m @@ -18,18 +18,24 @@ NSString* const CSContainerExceptionTypeKey = @"SCContainerExceptionTypeKey"; NSString* const CSContainerExceptionNameKey = @"SCContainerExceptionNameKey"; +NSString* const CSContainerExceptionReasonKey = @"SCContainerExceptionReasonKey"; @implementation ContainerException + (void)raiseWithReason:(NSString *)reason type:(NSString *)type name:(nullable NSString *)name { NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{ - CSContainerExceptionTypeKey: type + CSContainerExceptionTypeKey: type, + CSContainerExceptionReasonKey: reason }]; + NSMutableString *details = [[NSMutableString alloc] init]; + [details appendFormat:@"TYPE=%@", type]; if (name) { userInfo[CSContainerExceptionNameKey] = name; + [details appendFormat:@" NAME=%@", name]; } + @throw [NSException exceptionWithName:@"SCBasicObjc.Exception" - reason:reason + reason:[NSString stringWithFormat:@"%@ -- %@", reason, details] userInfo:userInfo]; } @@ -42,7 +48,8 @@ + (void)catchException:(__attribute__((noescape)) void (^)(void))operation if (error) { NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{ NSLocalizedDescriptionKey: exception.reason, - NSLocalizedFailureReasonErrorKey: exception.name, + NSLocalizedFailureErrorKey: exception.name, + CSContainerExceptionReasonKey: exception.userInfo[CSContainerExceptionReasonKey], CSContainerExceptionTypeKey: exception.userInfo[CSContainerExceptionTypeKey] }]; NSString *name = exception.userInfo[CSContainerExceptionNameKey]; diff --git a/Tests/SCInject/ContainerTests.swift b/Tests/SCInject/ContainerTests.swift index 2985762..8883c08 100644 --- a/Tests/SCInject/ContainerTests.swift +++ b/Tests/SCInject/ContainerTests.swift @@ -102,7 +102,7 @@ final class ContainerTests: XCTestCase { XCTAssertNoThrow(try container.validate()) } - func testValidate_missingNamedType() { + func testValidate_missingNamedType() throws { // Given let second: RegistrationName = .init(rawValue: "second") let container = DefaultContainer() @@ -116,9 +116,9 @@ final class ContainerTests: XCTestCase { // When / Then XCTAssertThrowsError(try container.validate()) { error in let error = error as? ContainerError - XCTAssertEqual(error?.reason, "Failed to resolve given type") + XCTAssertEqual(error?.reason, "Failed to resolve given type -- TYPE=TestClass1 NAME=second") XCTAssertEqual(error?.type, "TestClass1") - XCTAssertNil(error?.name) + XCTAssertEqual(error?.name, second.rawValue) } } }