How to describe error from Alamofire using switch case in Swift? - ios

I want to give info to the user about the error that occurred while sending a request to the server. I use Alamofire.
The code is like below:
Alamofire.request(url, method: methodUsed, parameters: parameters).responseData { (response) in
switch response.result {
case .failure(let error) :
// I want to the describe the error in here
case .success(let value) :
let json = JSON(value)
completion(.success(json))
}
}
I have tried but I can't switch the error. I want something similar to this to be placed in the code above:
switch error {
case .NoSignal : // give alert to the user about the signal
case .ServerError : // give alert to the user about server error
}
For some case I want to inform the user to take some action on the alert but I don't know what the available cases are and I don't know the syntax that has to be used.

As per Jayesh Thanki says you can identify the server error using status code and for internet connectivity you can use NetworkReachabilityManager of Alamofire. Write following code in viewDidLoad():
var networkManager: NetworkReachabilityManager = NetworkReachabilityManager()!
networkManager.startListening()
networkManager.listener = { (status) -> Void in
if status == NetworkReachabilityManager.NetworkReachabilityStatus.notReachable {
print("No internet available")
}else{
print("Internet available")
}

You can identify error using status code. response.response.statusCode.
There is lots of HTTP status code and using them you can inform end user with alert.
Here is wikipedia link for list of status code.
For example is status code is 200 OK then its successful HTTP request
and status code is 500 Internal Server Error then its server related error.
You can also provide error description using response.result.error.localizedDescription if error is available.

Related

How to customize Amplify Auth Error Messages

I am attempting to implement amplify auth on iOS, and what I would like to be able to do is customize the error message that is displayed to a user when authentication fails, as the default error messages are not end-user friendly, but I have no idea how to do this.
For instance, my signIn method is as follows:
func signIn(username: String) {
Amplify.Auth.signIn(username: username, password: "bla") { [weak self] result in
switch result {
case .success (let result):
if case .confirmSignInWithCustomChallenge(_) = result.nextStep {
DispatchQueue.main.async {
self?.showConfirmationSignInView()
}
} else {
print("Sign in succeeded")
}
case .failure(let error):
print (error)
}
}
}
Now in the .failure case, instead of printing the error, I would ideally like to determine if the error is a userNotFound error, or something else. I can't find any info in the docs on this. Any help would be appreciated.
You can do it by checking the error.code. for example, for a user who did not confirm the email if he tries to login then error.code will have UserNotConfirmedException string value. Amplify auth returns different exception codes for different types of errors. You can see all the exceptions from this link. Although it is for flutter, the exception code is identical for any framework. I have used these exception codes in react.

The operation couldn’t be completed. (AWSMobileClient.AWSMobileClientError error 20.)

I'm currently running into this error when implementing the AWSMobileClient signUp function. I haven't really altered the code sample from the AWS page describing how to implement it, other than changing the attributes to fit my user pool attribute requirements.
First in viewDidLoad, I initialize the mobile client like so:
AWSMobileClient.sharedInstance().initialize { (userState, error) in
if let userState = userState {
print("UserState: \(userState.rawValue)")
} else if let error = error {
print("error: \(error.localizedDescription)")
}
}
Then I have the function for signing up. This is what the code looks like (I encapsulate this in a function called signUpUser):
AWSMobileClient.sharedInstance().signUp(username: userEmail,
password: userPass,
userAttributes: ["email":userEmail, "given_name":userFirstName, "family_name": userLastName, "custom:school":userSchool]) { (signUpResult, error) in
if let signUpResult = signUpResult {
switch(signUpResult.signUpConfirmationState) {
case .confirmed:
print("User is signed up and confirmed.")
case .unconfirmed:
print("User is not confirmed and needs verification via \(signUpResult.codeDeliveryDetails!.deliveryMedium) sent at \(signUpResult.codeDeliveryDetails!.destination!)")
case .unknown:
print("Unexpected case")
}
} else if let error = error {
if let error = error as? AWSMobileClientError {
switch(error) {
case .usernameExists(let message):
print(message)
default:
break
}
}
print("\(error.localizedDescription)")
}
When I run the app on my iPhone, I call this function when the "Sign Up Button" is clicked. In the debug window, I get the following error:
The operation couldn’t be completed. (AWSMobileClient.AWSMobileClientError error 20.)
That's the only info that appears in the Xcode console. Does anyone know how to go about debugging or fixing this?
EDIT: I'm not sure what the issue was that caused this error. I started a fresh project, set up a new cognito pool and backend services, and ported over the code from this project, which resulted in everything working perfectly. The error may have been from incorrectly setting up the user pool, or perhaps not allowing unauthorized access to the sign up function (not sure if I had that set to "No").
If you exhaust the rest of the switch case there, you will be able to see what exactly is the error coming back from the service.
reference: https://stackoverflow.com/a/59521025/2464632

Why calls swiftHTTP the response handler if there is no connection?

I'm using swiftHTTP for requesting to my server and when my internet connection is slow, it goes to response part! I've set the example code below:
HTTP.GET("myURL") { response in
let myResponse = response.data // it comes here after the timeout
if response.statusCode == 200 {
//some code
} else {
do {
let jsonError = try JSON(data: myResponse) // in this line it goes to catch because there is no data in myresponse
} catch{
//alert for not having connection
}
}
Why does it call the response function if there's no response?
My server also says, that no request was sent.
It doesn't "go to response", it tries to make the HTTP request as expected and regardless of success or error it's completion handler is called.
The response object that is returned is an object that contains all of the information you need to determine what happened with the request.
So it will contain a timeout status code (HTTP 408), possibly an error message. If it did not respond at all, your app would not be able to handle these cases.
Say for example your user taps on their profile icon in the app and this sends a request to get the users profile, but it timed out. Do you want the user sat waiting, looking at a blank profile screen? It's much better to catch the error and handle it gracefully. In this case you could present a message to the user telling them that something went wrong and close the empty profile screen
Your response handler will also be called from swiftHTTP, if there's no or a very bad connection.To solve this problem, either check if there is an internet connection or check if the data is nil:
HTTP.GET("myURL") { response in
let myResponse = response.data // it comes here after the timeout
if response.statusCode == 200 || response.data == nil {
//some code
} else {
do {
let jsonError = try JSON(data: myResponse) // in this line it goes to catch because there is no data in myresponse
} catch{
//alert for not having connection
}
}
The important part here is the check if response.data == nil.

RxMoya request using mvvm model always crashes in observer.onError(error)

Following is my code for signing up
self.signedUp = signUpButtonTap.withLatestFrom(userAndPassword).flatMapLatest{
input -> Observable<Response> in
return Observable.create { observer in
let userData = Creator()
userData?.username = input.0
userData?.password = input.1
provider.request(.signIn(userData!)).filter(statusCode: 200).subscribe{ event -> Void in
switch event {
case .next(let response):
observer.onNext(response)
case .error(let error):
let moyaError: MoyaError? = error as? MoyaError
let response: Response? = moyaError?.response
let statusCode: Int? = response?.statusCode
observer.onError(error)
default:
break
}
}
return Disposables.create()
}
}
Following is the binding in the View
self.viewModel.signedUp.bind{response in
self.displayPopUpForSuccessfulLogin()
}
When there is a successful response its works fine.
But when the request times out or I get any other status code than 200, I get the following error "fatalError(lastMessage)" and the app crashes.
When I replace observer.onError(error) with observer.onNext(response) in the case .error it works for response codes other than 200 , but crashes again when the request times out.
I have gone through this link Handling Network error in combination with binding to tableView (Moya, RxSwift, RxCocoa)
Can anyone help me out with what is wrong. I am completely new to RxSwift . Any help will be appreciated. Thank you
If provider.request(.signIn(userData!)) // ... returns results on some background thread, results would be bound to UI elements from a background thread which could cause non-deterministic crashes.
It should be
provider.request(.signIn(userData!))
.observeOn(MainScheduler.instance) // ...
according to RxSwift github tips: Drive

Error handler not called for promise

I have a service, which fails when I enter bad login credentials. However, my Promise error handler does not get called.
I don't seem to grasp what's wrong with my code so that the error callback is never reached.
Service
func loadRepositories() -> Promise<[Repository]>{
return Promise { fullfill, reject in
manager.request(Method.GET, baseURL + "/api/1.0/user/repositories")
.authenticate(user: username, password: password)
.responseArray { (response: Response<[Repository], NSError>) in
switch response.result{
case .Success(let value):
fullfill(value)
case .Failure(let e):
// The breakpoint here is reached.
reject(e)
}
}
}
}
Handling
firstly{
service!.loadRepositories()
}.then { repositories -> Void in
loginVC.dismissViewControllerAnimated(true, completion: nil)
self.onLoginSuccessful()
}.always{
// Always gets called
loginVC.isSigningIn = false
}.error { error in
// I never get here even though `reject(e)` is called from the service.
loginVC.errorString = "Login went wrong"
}
By default error does not handle cancellation errors and bad credentials is exactly a cancellation error. If you put print(e.cancelled) before reject(e), you will see that it will return true. If you give a wrong URL for example, you will receive false. In order to get around this, just replace
}.error { error in
with:
}.error(policy: .AllErrors) { error in
and error will be triggered then. In case you use recover, cancellation errors will be handled by default. You can check https://github.com/mxcl/PromiseKit/blob/master/Sources/Promise.swift#L367 for more information.

Resources