Closed as not planned
Description
Question
I would like to write one wrapError
function that takes a generic Output
and handles all the errors. For this I think we'd need to have a protocol to which all Output
enums conform to. For instance:
enum OpenAPIResult<V: Sendable>: Sendable, Hashable {
case ok(value: V)
case badRequest(error: Components.Responses.ErrorResponse)
case unauthorized(error: Components.Responses.ErrorResponse)
case internalServerError(error: Components.Responses.ErrorResponse)
case undocumented(statusCode: Int)
// ... additional cases
}
func get(id: Int) -> AnyPublisher<[Comment], ErrorResponse> {
let response: Future<[Components.Schemas.Comment], ErrorResponse> = self.wrapError {
try await self.client?.GetComments(path: .init(id: id)) as? OpenAPIResult<[Components.Schemas.Comment]>
}
return response
.compactMap { arr -> [Comment] in
arr.compactMap { $0.comment }
}
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
func wrapError<V>(_ call: @escaping () async throws -> OpenAPIResult<V>) -> Future<V, ErrorResponse> {
// Call completion and log errors for all the routes
}
I am not an advanced enough Swift programmer to know if this would actually work. I'm trying to avoid copying error handling logic for each of my routes. Perhaps you have any other solutions?
Many thanks