Skip to content
Open
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
19 changes: 12 additions & 7 deletions Sources/SmokeOperations/OperationHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public struct OperationHandler<ContextType, RequestHeadType, InvocationReporting
= (_ input: InputType, _ requestHead: RequestHeadType, _ context: ContextType,
_ responseHandler: ResponseHandlerType, _ invocationContext: SmokeInvocationContext<InvocationReportingType>) -> ()
public typealias OperationResultDataInputFunction
= (_ requestHead: RequestHeadType, _ body: Data?, _ context: PerInvocationContext<ContextType, InvocationReportingType>,
= (_ requestHead: RequestHeadType, _ body: Data?, _ context: PerInvocationContext<ContextType, InvocationReportingType, OperationIdentifer>,
_ responseHandler: ResponseHandlerType, _ invocationStrategy: InvocationStrategy,
_ requestLogger: Logger, _ internalRequestId: String, _ invocationReportingProvider: (Logger) -> InvocationReportingType) -> ()

Expand All @@ -39,7 +39,7 @@ public struct OperationHandler<ContextType, RequestHeadType, InvocationReporting
* Handle for an operation handler delegates the input to the wrapped handling function
* constructed at initialization time.
*/
public func handle(_ requestHead: RequestHeadType, body: Data?, withContext context: PerInvocationContext<ContextType, InvocationReportingType>,
public func handle(_ requestHead: RequestHeadType, body: Data?, withContext context: PerInvocationContext<ContextType, InvocationReportingType, OperationIdentifer>,
responseHandler: ResponseHandlerType, invocationStrategy: InvocationStrategy,
requestLogger: Logger, internalRequestId: String,
invocationReportingProvider: @escaping (Logger) -> InvocationReportingType) {
Expand All @@ -53,8 +53,11 @@ public struct OperationHandler<ContextType, RequestHeadType, InvocationReporting
case error(description: String, reportableType: String?, invocationContext: SmokeInvocationContext<InvocationReportingType>)

func handle<OperationDelegateType: OperationDelegate>(
requestHead: RequestHeadType, context: PerInvocationContext<ContextType, InvocationReportingType>,
responseHandler: ResponseHandlerType, operationDelegate: OperationDelegateType)
requestHead: RequestHeadType,
context: PerInvocationContext<ContextType, InvocationReportingType, OperationIdentifer>,
responseHandler: ResponseHandlerType,
operationDelegate: OperationDelegateType,
operationIdentifier: OperationIdentifer)
where RequestHeadType == OperationDelegateType.RequestHeadType,
InvocationReportingType == OperationDelegateType.InvocationReportingType,
ResponseHandlerType == OperationDelegateType.ResponseHandlerType {
Expand Down Expand Up @@ -103,7 +106,7 @@ public struct OperationHandler<ContextType, RequestHeadType, InvocationReporting
case .static(let staticContext):
contextForInvocation = staticContext
case .provider(let contextProvider):
contextForInvocation = contextProvider(invocationContext.invocationReporting)
contextForInvocation = contextProvider(invocationContext.invocationReporting, operationIdentifier)
}

inputHandler(input, requestHead, contextForInvocation, responseHandler, invocationContext)
Expand Down Expand Up @@ -212,14 +215,16 @@ public struct OperationHandler<ContextType, RequestHeadType, InvocationReporting
requestHead: requestHead,
context: context,
responseHandler: responseHandler,
operationDelegate: operationDelegate)
operationDelegate: operationDelegate,
operationIdentifier: operationIdentifer)
} else {
invocationStrategy.invoke {
inputDecodeResult.handle(
requestHead: requestHead,
context: context,
responseHandler: responseHandler,
operationDelegate: operationDelegate)
operationDelegate: operationDelegate,
operationIdentifier: operationIdentifer)
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/SmokeOperations/OperationHandlerExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public extension OperationHandler {
return OperationFailure(code: code,
error: error)
}

if let knownError = error as? SmokeKnownError,
knownError.isErrorType(errorIdentity: .unrecognizedErrorFromExternalCall) {
return OperationFailure(code: 500,
error: error)
}

return nil
}
Expand Down
10 changes: 8 additions & 2 deletions Sources/SmokeOperations/PerInvocationContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@

import Foundation

public enum PerInvocationContext<ContextType, InvocationReportingType> {
public enum PerInvocationContext<ContextType, InvocationReportingType, OperationIdentity> {
case `static`(ContextType)
case provider((InvocationReportingType) -> ContextType)
case provider((InvocationReportingType, OperationIdentity) -> ContextType)
}

public protocol OperationAwarePerInvocationContext {
associatedtype OperationIdentity

var operationIdentifier: OperationIdentity? { get }
}
8 changes: 8 additions & 0 deletions Sources/SmokeOperations/ReturnableErrorProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public protocol SmokePerInvocationContextInitializer {
var reportingConfiguration: SmokeReportingConfiguration<SelectorType.OperationIdentifer> { get }

func getInvocationContext(invocationReporting: InvocationReportingType) -> SelectorType.ContextType
func getInvocationContext(invocationReporting: InvocationReportingType,
operationIdentifier: SelectorType.OperationIdentifer) -> SelectorType.ContextType

func onShutdown() throws
}
Expand All @@ -55,4 +57,9 @@ public extension SmokePerInvocationContextInitializer {
var reportingConfiguration: SmokeReportingConfiguration<SelectorType.OperationIdentifer> {
return SmokeReportingConfiguration()
}

func getInvocationContext(invocationReporting: InvocationReportingType,
operationIdentifier: SelectorType.OperationIdentifer) -> SelectorType.ContextType {
return getInvocationContext(invocationReporting: invocationReporting)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public protocol SmokePerInvocationContextInitializerV2 {
var reportingConfiguration: SmokeReportingConfiguration<SelectorType.OperationIdentifer> { get }

func getInvocationContext(invocationReporting: InvocationReportingType) -> SelectorType.ContextType
func getInvocationContext(invocationReporting: InvocationReportingType,
operationIdentifier: SelectorType.OperationIdentifer) -> SelectorType.ContextType

func onShutdown() throws
}
Expand All @@ -61,4 +63,9 @@ public extension SmokePerInvocationContextInitializerV2 {
var reportingConfiguration: SmokeReportingConfiguration<SelectorType.OperationIdentifer> {
return SmokeReportingConfiguration()
}

func getInvocationContext(invocationReporting: InvocationReportingType,
operationIdentifier: SelectorType.OperationIdentifer) -> SelectorType.ContextType {
return getInvocationContext(invocationReporting: invocationReporting)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public struct StandardHTTP1OperationRequestHandler<SelectorType>: HTTP1Operation
public typealias InvocationReportingType = SelectorType.DefaultOperationDelegateType.InvocationReportingType

let handlerSelector: SelectorType
let context: PerInvocationContext<SelectorType.ContextType, InvocationReportingType>
let context: PerInvocationContext<SelectorType.ContextType, InvocationReportingType, SelectorType.OperationIdentifer>
let pingOperationReporting: SmokeOperationReporting
let unknownOperationReporting: SmokeOperationReporting
let errorDeterminingOperationReporting: SmokeOperationReporting
Expand All @@ -62,7 +62,7 @@ public struct StandardHTTP1OperationRequestHandler<SelectorType>: HTTP1Operation
}

public init(handlerSelector: SelectorType,
contextProvider: @escaping (InvocationReportingType) -> SelectorType.ContextType,
contextProvider: @escaping (InvocationReportingType, SelectorType.OperationIdentifer) -> SelectorType.ContextType,
serverName: String, reportingConfiguration: SmokeReportingConfiguration<SelectorType.OperationIdentifer>) {
self.handlerSelector = handlerSelector
self.context = .provider(contextProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public extension SmokeHTTP1Server {

static func startAsOperationServer<SelectorType: SmokeHTTP1HandlerSelector, TraceContextType>(
withHandlerSelector handlerSelector: SelectorType,
andContextProvider contextProvider: @escaping (SmokeServerInvocationReporting<TraceContextType>) -> SelectorType.ContextType,
andContextProvider contextProvider: @escaping (SmokeServerInvocationReporting<TraceContextType>, SelectorType.OperationIdentifer) -> SelectorType.ContextType,
andPort port: Int = ServerDefaults.defaultPort,
serverName: String = "Server",
invocationStrategy: InvocationStrategy = GlobalDispatchQueueAsyncInvocationStrategy(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct OperationServerHTTP1RequestHandler<SelectorType, TraceContextType>: HTTP1
}

init(handlerSelector: SelectorType,
contextProvider: @escaping (InvocationReportingType) -> SelectorType.ContextType,
contextProvider: @escaping (InvocationReportingType, SelectorType.OperationIdentifer) -> SelectorType.ContextType,
serverName: String, reportingConfiguration: SmokeReportingConfiguration<SelectorType.OperationIdentifer>) {
self.operationRequestHandler = StandardHTTP1OperationRequestHandler(
handlerSelector: handlerSelector,
Expand Down