How to avoid re-parsing Error when using enum that conforms to Error protocol in Swift - swift

There's an error enum:
#objc public enum MyError: Int, Error {
case good
case bad
case ugly
}
Note that it's available to objective C - that's part of the challenge. For instance I cannot change enum to have associated values (like case good(Error))
A decent solution is to have a constructor that can parse from some arbitrary error:
init(from error: Error?) {
// parse it and set self to corresponding code, e.g.
guard let error = error else {
self = .good
return
}
if let uglyError = error as NSError? {
self = .ugly
return
}
self = .bad
}
But one of the cases this constructor has to handle is that provided error is already one of MyError values, i.e. Error(MyError.ugly) - this is simplification of course. More likely it's a result of lower-level error bubbling up.
In that case I want to avoid re-parsing it. How is that possible?
In pseudo-code I want to implement condition like this:
if error is [one of MyError values] {
self = error
return
}
Tried to do that with CaseIterable and allCases, but seems cannot convert Error to something that can be compared
for value in MyError.allCases {
if error == value { // <-- Error: Binary operator '==' cannot be applied to operands of type 'Error' and 'MyError'
}
}
Any way to accomplish something like that? (I am not stuck on CaseIterable, anything else works too).

If you are aiming to keep the same initializer as init(from error: Error?), what you could do is to add
if let castedError = error as? MyError {
self = castedError
return
}
at the beginning (before doing anything).
As a full implementation, it should be similar to:
#objc public enum MyError: Int, Error, CaseIterable {
case good
case bad
case ugly
init(from error: Error?) {
if let castedError = error as? MyError {
print("Already MyError!")
self = castedError
return
}
guard let error = error else {
self = .good
return
}
if let _ = error as NSError? {
self = .ugly
return
}
self = .bad
}
}
Therefore, the output would be:
let givenError: MyError = .good
let resultError = MyError(from: givenError)
print(resultError.rawValue) // 0 (which is .good raw value)
Note that it should also log "Already MyError!" because of the print statement in the first check to confirm that it has been reached.
It should also behave as expected with NSErrors:
let nsError = NSError(domain: "", code: 101, userInfo: nil)
let myError = MyError(from: nsError)
print(myError.rawValue) // 2 (which is .ugly raw value)

Related

Swift Combine framework setFailureType error operator

For scientific reasons I've created a Publisher and a Subscriber so I can dive into Combine.
The Publisher has been converted from a never failing to the failing one.
enum IntegerError: String, Error {
case miltupleOf2 = "We are sorry but the number is a multiple of 2, therefore cannot be used in the process"
}
let integerPublisher = [1,3,3,3,3,3,5,6,7,7].publisher
.setFailureType(to: IntegerError.self)
let subscribtion = integerPublisher
.tryMap { intValue in
if intValue.isMultiple(of: 2) {
throw IntegerError.miltupleOf2
} else {
return intValue
}
}
.sink { completion in
switch completion {
case .finished:
print("success")
case .failure(let error):
if let error = error as? IntegerError {
print(error.rawValue)
} else {
print(error)
}
}
} receiveValue: { value in
print(value)
}
My question is: when using sink, the error type is Error. Why is it not the custom IntegerError that I've used within the .setFailureType modifier?
The need of casting my error to the type that I specified earlier seems a little redundant.
Thank you.
The reason for this is quite straightforward. tryMap returns a Publishers.TryMap<Upstream, Output>, which is a specific kind of publisher with Failure == Error:
typealias Failure = Error
So as soon as you use tryMap, that undoes what setFailureType did.
The reason why Publishers.TryMap has Error as its Failure type is because in Swift, you can't specify what specific type of error a closure can throw (unlike Java for example). Once you mark a closure as throws, like tryMap has done with its transform parameter:
func tryMap<T>(_ transform: #escaping (Self.Output) throws -> T) -> Publishers.TryMap<Self, T>
any Error can be thrown inside the transform closure. You can try changing throw IntegerError.miltupleOf2 to throwing another type of error. Your code will still compile.
Hypothetically, if Swift allowed you to specify what type of error you are allowed to throw in the closure, then tryMap could have been declared as (fake syntax):
func tryMap<T, E: Error>(_ transform: #escaping (Self.Output) throws E -> T) -> Publishers.TryMap<Self, T, E>
and you wouldn't even need setFailureType.
As a workaround, you can use mapError to cast the error to your desired type:
let subscribtion = integerPublisher
.tryMap { intValue -> Int in
if intValue.isMultiple(of: 2) {
throw IntegerError.miltupleOf2
} else {
return intValue
}
}.mapError { $0 as! IntegerError }
.sink { completion in
switch completion {
case .finished:
print("success")
case .failure(let error):
print(error.rawValue)
}
} receiveValue: { value in
print(value)
}
You need to reassure the compiler that you haven't thrown any other type of errors in tryMap.

Set value of array via KeyPath

I am trying to update the value stored in an array property of a class via the use of KeyPaths. Here is my code:
func listenAndUpdateDocuments<T: JSONDecodable>(
_ property: ReferenceWritableKeyPath<MyModel, [T]?>,
from model: MyModel) {
guard let reference = reference else {
return
}
guard listener == nil else {
return
}
listener = backendClient.listenToDocuments(reference) { [weak model] (result: Result<[T], RequestError>) in
switch result {
case .success(let value):
model?[keyPath: property] = value
case .failure:
model?[keyPath: property] = []
}
}
}
The problem is when I call this function like this:
myListener.listenAndUpdateDocuments(\.viewers, from: self)
where viewers is of type [ViewersModel], it always comes back with the following error:
Type of expression is ambiguous without more context
How do I solve this? I have a similar version of the code but where the property parameter isn't an array, and that works.
I struggled with something similar:
_ = Token.query(on: req).filter(\.expiry < Date()).delete()
The solution I found was to use a more up-to-date api to handle my request parameters.
_ = Token.query(on: req).filter(\.expiry, .lessThan, Date()).delete()
It had less to do with the keypath itself than I thought!

Getting error when trying to use Result type with delegate

Im tring to make a network call and instead of using callback I try to use delegate instead.using Result type where .Sucsess is T: Decodable and .failure is Error. passing my model in the .Sucsess is working but when trying to pass an error I get a compile error "Generic parameter 'T' could not be inferred" what am I missing ?
protocol NetworkServiceDelegate: class {
func decodableResponce<T: Decodable>(_ result: Result<T, NetworkError>)
}
let dataTask:URLSessionTask = session.dataTask(with: url) { (dataOrNil, responceOrNil, errOrNil) in
if let error = errOrNil {
switch error {
case URLError.networkConnectionLost,URLError.notConnectedToInternet:
print("no network connection")
self.delegate?.decodableResponce(Result.failure(.networkConnectionLost))
case URLError.cannotFindHost, URLError.notConnectedToInternet:
print("cant find the host, could be to busy, try again in a little while")
case URLError.cancelled:
// if cancelled with the cancelled method the complition is still called
print("dont bother the user, we're doing what they want")
default:
print("error = \(error.localizedDescription)")
}
return
}
guard let httpResponce:HTTPURLResponse = responceOrNil as? HTTPURLResponse
else{
print("not an http responce")
return
}
guard let dataResponse = dataOrNil,
errOrNil == nil else {
print(errOrNil?.localizedDescription ?? "Response Error")
return }
do{
//here dataResponse received from a network request
let decoder = JSONDecoder()
let modelArray = try decoder.decode([Movie].self, from:
dataResponse) //Decode JSON Response Data
DispatchQueue.main.async {
self.delegate?.decodableResponce(Result.success(modelArray))
}
} catch let parsingError {
print("Error", parsingError)
}
print("http status = \(httpResponce.statusCode)")
print("completed")
}
this line generates the error, it dosnt metter if I pass my enum that cumfirms to Error or trying to pass the error from the dataTask
self.delegate?.decodableResponce(Result.failure(.networkConnectionLost))
Well, you have two problems, having to do with the question "what type is this?" Swift is very strict about types, so you need to get clear about that.
.networkConnectionLost is not an Error. It is an error code. You need to pass an Error object to a Result when you want to package up the error. For example, URLError(URLError.networkConnectionLost) is an Error.
The phrase Result<T, NetworkError> makes no sense. Result is already a generic. Your job is to resolve the generic that it already is. You do that by specifying the type.
So for example, you might declare:
func decodableResponce(_ result: Result<Decodable, Error>)
It is then possible to say (as tests):
decodableResponce(.failure(URLError(URLError.networkConnectionLost)))
or (assuming Movie is Decodable):
decodableResponce(.success([Movie()]))
That proves we have our types right, and you can proceed to build up your actual code around that example code.

Confused on Error Handling in Swift 3

I got confused error handling in swift3. I try to do like "if XX function got error then try YY function"
Let me show you what I try:
class MyClass {
enum error: Error
{
case nilString
}
func findURL() {
do {
let opt = try HTTP.GET(url_adr!)
opt.start { response in
if let err = response.error {
print("error: \(err.localizedDescription)")
return //also notify app of failure as needed
}
do
{
/* This is func1. and got error. I want to if this function has error then go next function. */
try self.stringOperation(data: response.description)
}
catch{
print("doesn't work on func1. trying 2nd func")
self.stringOperation2(data:response.descritption)
}
}
} catch let error {
print("got an error creating the request: \(error)")
}
}
func stringOperation(data:String)throws -> Bool{
do{
/** 1 **/
if let _:String = try! data.substring(from: data.index(of: "var sources2")!){
print("its done")
}else{
throw error.nilString
}
IN 1: I got this fatal error on this line:
"fatal error: unexpectedly found nil while unwrapping an Optional value" and program crashed.
I googled error handling try to understand and apply to in my code. However not succeed yet. Can someone explain where did I wrong?
Additional info: I got String extension for .substring(from:...) , and .index(of:"str"). So these lines doesn't got you confused.
As a general rule, try avoiding using force unwrapping (!), where you have
if let _: String= try! data.substring...
Instead use
if let index = data.index(of: "var sources2"),
let _: String = try? data.substring(from: index) { ... } else { ... }
That way you remove the two force unwraps that may be causing your crash. You already have the if let protection for catching the nil value, so you can make the most of it by using the conditional unwrapping.

Generic perform Request, using Generics

I would like to make a perform request function in swift using Generics. I want to make the call and switch on my enum Result based on what I get back. However, I don't understand the : 'cannot invoke performRequest with an argument list of type (NSURLRequest, (Result<__>) -> ())' Why can't I have an unnamed parameter here? I have also tried something like the following : r<MyStruct> --- but I then get an expected expression error. Any help explaining the above Result<_> error would be greatly appreciated. Thanks.
enum Result<A> {
case Value
case Error
}
func performRequest<A>(request:NSURLRequest, callback:(Result<A>) -> ()) {
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
callback(parseResponse(data, response: response, error: error))
}
task.resume()
}
class SampleClass {
let request = NSURLRequest(URL: NSURL(string: "www.google.com")!)
init() {
performRequest(request) { r in -------- errors out
switch r {
case .Value:
case .Error:
}
}
}
The problem is that when you use performRequest, you have not given the compiler enough information about the generic parameter you intend to use. The critical part that is missing is that parseResponse needs to return a Result that is parameterised in the same way as the callback. However, in the snippet you provided, parseResponse is not generic.
I believe this will do what you intend. In this scenario, I've parameterised the Result with String, but you can substitute any other type.
// multi-purpose (generic) Result type
enum Result<A>
{
case Value(A) // because you parameterised the enum, you might as well take advantage of the type
case Error
}
// this is a custom parser, you may substitute your own that returns a different type
func parseString( data:NSData?, response:NSURLResponse?, error:NSError? ) -> Result<String> {
if let _ = error {
return Result.Error
}
return Result.Value("Success")
}
// this function is completely generic, but the parser and callback need to be compatible
func performRequest<A>( request:NSURLRequest,
parser:( NSData?, NSURLResponse?, NSError? ) -> Result<A>,
callback:( Result<A> ) -> Void ) {
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
( data, response, error ) -> Void in
callback( parser( data, response, error ) )
}
task.resume()
}
let request = NSURLRequest(URL: NSURL(string: "www.google.com")!)
// actual invocation, now I need to pass in a concrete parser and callback with a specific type
performRequest( request, parser: parseString ) { // parseString returns a Result<String>
r in
switch r {
case .Value( let value ):
// because I passed in a parser that returns a Result<String>, I know that "value" is a String here
print( "Succeeded with value: \(value)" )
break;
case .Error:
print( "an error occurred" )
break;
}
}