Handling errors properly in swift - ios

I am trying to learn swift and I have hit a wall... I want to be able to switch the type of error i get back so I can do different things. It works fine in the .success but not in the .failure
exporter.export(progressHandler: { (progress) in
print(progress)
}, completionHandler: { result in
switch result {
case .success(let status):
switch status {
case .completed:
break
default:
break
}
break
case .failure(let error):
// I want to check what the error is
// e.g. the debugger says its "cancelled"
break
}
})
}
Can somebody help me with this?
Thanks

If you just want to see what happened, print the error object's localizedDescription.
print(error.localizedDescription)
If you have a decision to make, cast to NSError and examine the domain and code. That is more reliable though not as user-friendly. Only actual testing will tell you what the possible values are.
let error = error as NSError
if error.domain == ... && error.code == ... {
You can work out the corresponding Swift Error type by looking in the FoundationErrors.h header file. Once you do, you can refine your case structure to filter the error type into its own case:
case .failure(let error as NextLevelSessionExporterError):
// do something
case .failure(let error):
// do something else

Related

How can I call this Closure in Swift 5

I'm still new in Swift development and I'm wondering how can I call This function with the status and results?
func getStatus(completion: #escaping (Swift.Result<SubscriptionStatus, MAPIError>) -> Void )
{
getStatus { result in
switch(result) {
case .success(let subscription):
switch(subscription.status) {
case .subscribed:
completion(.success(true))
default:
completion(.success(false))
}
case .failure(let error):
completion(.failure(error))
}
}
}
Many Thanks
Calling the function
Result is an enum which can be either:
success
or
failure
So, completion(.success(value)) or completion(.failure(someError))
Result is such a powerful keyword in swift!
It determines wether the task should succeed or fail.
To complete your function you should call it like this:
completion(.success(SubscriptionStatusValue))
If the task succeed
Or
Completion(.failure(error))
To call your function you should do it like this:
self.getStatus { result in
switch result {
case .success(let value):
/// do something with value
case .failure(let error):
///handle error
Hope it helps!!

Always getting Tabby.CheckoutError.invalidResponse

I have been trying to start session using Tabby SDK in iOS swiftui
Every Thing is setup as guided.
.onAppear I'm running this code
let myTestPayment = TabbyCheckoutPayload(merchant_code: "ae", lang: .en, payment: customerPayment)
TabbySDK.shared.configure(forPayment: myTestPayment) { result in
switch result {
case .success(let s):
//Do something
case .failure(let error):
print(error)
}
}
where customerPayment is struct of params accepted by Tabby. I always get this error The operation couldn’t be completed. (Tabby.CheckoutError error 1.)
Could you please guide me what's the issue.
Is Tabby Payments banned in Pakistan?

Update to Xcode 12: Cannot convert value of type 'DataRequest' to closure result type 'Void'

I am using AlamoFire and PromiseKit to do API calls.
The code was working fine for 2 years until I updated to Xcode 12.0.
Function now returning the error: "Cannot convert value of type 'DataRequest' to closure result type 'Void'"
My function is as follows:
func fetchArticlesFromApi (API: String) -> Promise<[Article]> {
return Promise<[Article]> { seal in
return Alamofire.request(API).validate().responseString(completionHandler: { //Error happening here
response in
switch (response.result) {
case .success(let responseString1):
//Do something
case .failure(let error):
print (error)
seal.reject(error)
}
})
}
}
Error happening in third line of function
Any thoughts what might have changed in this update?
Note: The code works fine when i run the same code on xcode 11.6!
I found the answer for that on Github.
https://github.com/mxcl/PromiseKit/issues/1165
I shouldn't be trying to return anything from the closure passed to Promise.init.
Weird how that was working in previous versions of Xcode.
To fix that I have to replace the return in front of Alamofire.request... with _ =
The function now looks like this:
func fetchArticlesFromApi (API: String) -> Promise<[Article]> {
return Promise<[Article]> { seal in
_ = AF.request(API).validate().responseString(completionHandler: {
response in
switch (response.result) {
case .success(let responseString1):
//Do something
case .failure(let error):
print (error)
seal.reject(error)
}
})
}
}

Display user friendly message for json serialisation error

Iam working on an ios application which uses Alamofire for networking. Some times i get json serialization error. I need to display some user friendly messsage when this error comes. How can I do this by checking Nserror code.
Any help will be appreciated.
If your response gives serialization error then you can get status isFailure You can display some custom message or can user response.result.error object.
Alamofire.request(url, method: .post, parameters: param, encoding: URLEncoding.httpBody, headers: nil).responseSwiftyJSON { (response) in
if response.result.isSuccess {
// Handle your json
} else {
// display message Something went Wrong
}
}
You can actually check AFError.swift file to get familiar with possible values.
switch responseJson.result.error as? AFError{
case .responseSerializationFailed(let reason)?:
switch reason {
case .inputDataNil:
break
case .inputDataNilOrZeroLength:
break
case .inputFileNil:
break
case .inputFileReadFailed(let at):
break
case .stringSerializationFailed(let encoding):
break
case .jsonSerializationFailed(let error): break
***////// DO What ever you want here //////////////////***
case .propertyListSerializationFailed(let error):
break
}
case .some(.multipartEncodingFailed(let reason)):
break
case .some(.responseValidationFailed(let reason)):
break
case .some(.responseSerializationFailed(let reason)):
break
case .none:
<#code#>
case .some(.invalidURL(let url)):
<#code#>
}

compare error object return by alamofire

I'm using Alamofire with EVReflection, in case responseObject fails to parse the raw response string into an object, an response.error will have some value, in case of a different error, a different value will be set.
Not sure how to compare those error values, to handle different error.
in case of JSON parsing error, print(error) will output
FAILURE: Error Domain=com.alamofirejsontoobjects.error Code=1 "Data could not be serialized. Input data was not json." UserInfo={NSLocalizedFailureReason=Data could not be serialized. Input data was not json.}
Alamofire.request(...)
.responseObject { (response: DataResponse<UserData>) in
guard response.error == nil else {
print(response.error)
return
}
}
When your request fails, you will get an error of type AFError from Alamofire. You can actually check AFError.swift file to get familiar with possible values. This file have really good documentation for every case.
Since AFError is an Error, which is of type enum, you can check like following:
switch err {
case .parameterEncodingFailed(let reason):
// do something with this.
// If you want to know more - check for reason's cases like
// switch reason {
// case .jsonEncodingFailed(let error):
// … // handle somehow
// case .propertyListEncodingFailed(let error):
// … // handle somehow
// }
case .responseValidationFailed(let reason):
// do something else with this
…
}
And for every reason you have some helper functions, so you can get even more info. Just check documentation.

Resources