From b81f58fec500b4e248c6887cfae44d2cbf7938d7 Mon Sep 17 00:00:00 2001 From: PB Thiffault Date: Tue, 26 Oct 2021 12:00:27 -0400 Subject: [PATCH] Add operation identifier to invocation context; allow known errors to be returned from Smoke operations --- .../SmokeOperations/OperationHandler.swift | 19 ++++++++++++------- .../OperationHandlerExtensions.swift | 6 ++++++ .../PerInvocationContext.swift | 10 ++++++++-- .../ReturnableErrorProtocols.swift | 8 ++++++++ ...SmokePerInvocationContextInitializer.swift | 7 +++++++ ...okePerInvocationContextInitializerV2.swift | 7 +++++++ ...StandardHTTP1OperationRequestHandler.swift | 4 ++-- ...keHTTP1Server+startAsOperationServer.swift | 2 +- .../SmokeServerHTTP1RequestHandler.swift | 2 +- 9 files changed, 52 insertions(+), 13 deletions(-) diff --git a/Sources/SmokeOperations/OperationHandler.swift b/Sources/SmokeOperations/OperationHandler.swift index eef1bfe..d4689a8 100644 --- a/Sources/SmokeOperations/OperationHandler.swift +++ b/Sources/SmokeOperations/OperationHandler.swift @@ -29,7 +29,7 @@ public struct OperationHandler) -> () public typealias OperationResultDataInputFunction - = (_ requestHead: RequestHeadType, _ body: Data?, _ context: PerInvocationContext, + = (_ requestHead: RequestHeadType, _ body: Data?, _ context: PerInvocationContext, _ responseHandler: ResponseHandlerType, _ invocationStrategy: InvocationStrategy, _ requestLogger: Logger, _ internalRequestId: String, _ invocationReportingProvider: (Logger) -> InvocationReportingType) -> () @@ -39,7 +39,7 @@ public struct OperationHandler, + public func handle(_ requestHead: RequestHeadType, body: Data?, withContext context: PerInvocationContext, responseHandler: ResponseHandlerType, invocationStrategy: InvocationStrategy, requestLogger: Logger, internalRequestId: String, invocationReportingProvider: @escaping (Logger) -> InvocationReportingType) { @@ -53,8 +53,11 @@ public struct OperationHandler) func handle( - requestHead: RequestHeadType, context: PerInvocationContext, - responseHandler: ResponseHandlerType, operationDelegate: OperationDelegateType) + requestHead: RequestHeadType, + context: PerInvocationContext, + responseHandler: ResponseHandlerType, + operationDelegate: OperationDelegateType, + operationIdentifier: OperationIdentifer) where RequestHeadType == OperationDelegateType.RequestHeadType, InvocationReportingType == OperationDelegateType.InvocationReportingType, ResponseHandlerType == OperationDelegateType.ResponseHandlerType { @@ -103,7 +106,7 @@ public struct OperationHandler { +public enum PerInvocationContext { case `static`(ContextType) - case provider((InvocationReportingType) -> ContextType) + case provider((InvocationReportingType, OperationIdentity) -> ContextType) +} + +public protocol OperationAwarePerInvocationContext { + associatedtype OperationIdentity + + var operationIdentifier: OperationIdentity? { get } } diff --git a/Sources/SmokeOperations/ReturnableErrorProtocols.swift b/Sources/SmokeOperations/ReturnableErrorProtocols.swift index 9529f24..79c9ec6 100644 --- a/Sources/SmokeOperations/ReturnableErrorProtocols.swift +++ b/Sources/SmokeOperations/ReturnableErrorProtocols.swift @@ -37,3 +37,11 @@ extension Encodable where Self: SmokeReturnableError { return try errorEncoder.encode(self, logger: logger) } } + +public protocol SmokeKnownError: SmokeReturnableError { + func isErrorType(errorIdentity: SmokeKnownErrorIdentifiers) -> Bool +} + +public enum SmokeKnownErrorIdentifiers { + case unrecognizedErrorFromExternalCall +} diff --git a/Sources/SmokeOperationsHTTP1/SmokePerInvocationContextInitializer.swift b/Sources/SmokeOperationsHTTP1/SmokePerInvocationContextInitializer.swift index ba74df3..6e7f681 100644 --- a/Sources/SmokeOperationsHTTP1/SmokePerInvocationContextInitializer.swift +++ b/Sources/SmokeOperationsHTTP1/SmokePerInvocationContextInitializer.swift @@ -35,6 +35,8 @@ public protocol SmokePerInvocationContextInitializer { var reportingConfiguration: SmokeReportingConfiguration { get } func getInvocationContext(invocationReporting: InvocationReportingType) -> SelectorType.ContextType + func getInvocationContext(invocationReporting: InvocationReportingType, + operationIdentifier: SelectorType.OperationIdentifer) -> SelectorType.ContextType func onShutdown() throws } @@ -55,4 +57,9 @@ public extension SmokePerInvocationContextInitializer { var reportingConfiguration: SmokeReportingConfiguration { return SmokeReportingConfiguration() } + + func getInvocationContext(invocationReporting: InvocationReportingType, + operationIdentifier: SelectorType.OperationIdentifer) -> SelectorType.ContextType { + return getInvocationContext(invocationReporting: invocationReporting) + } } diff --git a/Sources/SmokeOperationsHTTP1/SmokePerInvocationContextInitializerV2.swift b/Sources/SmokeOperationsHTTP1/SmokePerInvocationContextInitializerV2.swift index 4133976..443208e 100644 --- a/Sources/SmokeOperationsHTTP1/SmokePerInvocationContextInitializerV2.swift +++ b/Sources/SmokeOperationsHTTP1/SmokePerInvocationContextInitializerV2.swift @@ -41,6 +41,8 @@ public protocol SmokePerInvocationContextInitializerV2 { var reportingConfiguration: SmokeReportingConfiguration { get } func getInvocationContext(invocationReporting: InvocationReportingType) -> SelectorType.ContextType + func getInvocationContext(invocationReporting: InvocationReportingType, + operationIdentifier: SelectorType.OperationIdentifer) -> SelectorType.ContextType func onShutdown() throws } @@ -61,4 +63,9 @@ public extension SmokePerInvocationContextInitializerV2 { var reportingConfiguration: SmokeReportingConfiguration { return SmokeReportingConfiguration() } + + func getInvocationContext(invocationReporting: InvocationReportingType, + operationIdentifier: SelectorType.OperationIdentifer) -> SelectorType.ContextType { + return getInvocationContext(invocationReporting: invocationReporting) + } } diff --git a/Sources/SmokeOperationsHTTP1/StandardHTTP1OperationRequestHandler.swift b/Sources/SmokeOperationsHTTP1/StandardHTTP1OperationRequestHandler.swift index 7ec701e..9db589e 100644 --- a/Sources/SmokeOperationsHTTP1/StandardHTTP1OperationRequestHandler.swift +++ b/Sources/SmokeOperationsHTTP1/StandardHTTP1OperationRequestHandler.swift @@ -42,7 +42,7 @@ public struct StandardHTTP1OperationRequestHandler: HTTP1Operation public typealias InvocationReportingType = SelectorType.DefaultOperationDelegateType.InvocationReportingType let handlerSelector: SelectorType - let context: PerInvocationContext + let context: PerInvocationContext let pingOperationReporting: SmokeOperationReporting let unknownOperationReporting: SmokeOperationReporting let errorDeterminingOperationReporting: SmokeOperationReporting @@ -62,7 +62,7 @@ public struct StandardHTTP1OperationRequestHandler: HTTP1Operation } public init(handlerSelector: SelectorType, - contextProvider: @escaping (InvocationReportingType) -> SelectorType.ContextType, + contextProvider: @escaping (InvocationReportingType, SelectorType.OperationIdentifer) -> SelectorType.ContextType, serverName: String, reportingConfiguration: SmokeReportingConfiguration) { self.handlerSelector = handlerSelector self.context = .provider(contextProvider) diff --git a/Sources/SmokeOperationsHTTP1Server/SmokeHTTP1Server+startAsOperationServer.swift b/Sources/SmokeOperationsHTTP1Server/SmokeHTTP1Server+startAsOperationServer.swift index f0b63e7..66829f4 100644 --- a/Sources/SmokeOperationsHTTP1Server/SmokeHTTP1Server+startAsOperationServer.swift +++ b/Sources/SmokeOperationsHTTP1Server/SmokeHTTP1Server+startAsOperationServer.swift @@ -55,7 +55,7 @@ public extension SmokeHTTP1Server { static func startAsOperationServer( withHandlerSelector handlerSelector: SelectorType, - andContextProvider contextProvider: @escaping (SmokeServerInvocationReporting) -> SelectorType.ContextType, + andContextProvider contextProvider: @escaping (SmokeServerInvocationReporting, SelectorType.OperationIdentifer) -> SelectorType.ContextType, andPort port: Int = ServerDefaults.defaultPort, serverName: String = "Server", invocationStrategy: InvocationStrategy = GlobalDispatchQueueAsyncInvocationStrategy(), diff --git a/Sources/SmokeOperationsHTTP1Server/SmokeServerHTTP1RequestHandler.swift b/Sources/SmokeOperationsHTTP1Server/SmokeServerHTTP1RequestHandler.swift index 9a6b2f9..f6ceb36 100644 --- a/Sources/SmokeOperationsHTTP1Server/SmokeServerHTTP1RequestHandler.swift +++ b/Sources/SmokeOperationsHTTP1Server/SmokeServerHTTP1RequestHandler.swift @@ -56,7 +56,7 @@ struct OperationServerHTTP1RequestHandler: HTTP1 } init(handlerSelector: SelectorType, - contextProvider: @escaping (InvocationReportingType) -> SelectorType.ContextType, + contextProvider: @escaping (InvocationReportingType, SelectorType.OperationIdentifer) -> SelectorType.ContextType, serverName: String, reportingConfiguration: SmokeReportingConfiguration) { self.operationRequestHandler = StandardHTTP1OperationRequestHandler( handlerSelector: handlerSelector,