Skip to content

Commit

Permalink
LoginVC: detailed login error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
BLeeEZ committed Feb 23, 2021
1 parent 1685a98 commit 85cb35f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Amperfy/Api/Ampache/AmpacheXmlServerApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ class AmpacheXmlServerApi {
let curDelegate = AuthParserDelegate()
parser.delegate = curDelegate
let success = parser.parse()
if let error = parser.parserError {
authHandshake = nil
os_log("Error during AuthPars: %s", log: log, type: .error, error.localizedDescription)
return
}
if success && curDelegate.authHandshake != nil {
authHandshake = curDelegate.authHandshake
} else {
Expand Down
49 changes: 46 additions & 3 deletions Amperfy/Api/BackendProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ enum BackenApiType: Int {
}
}

enum AuthenticationError: Error {
case notAbleToLogin
struct AuthenticationError: Error {
enum ErrorKind {
case notAbleToLogin
case invalidUrl
case requestStatusError
case downloadError
}

var message: String = ""
let kind: ErrorKind
}

class BackendProxy {
Expand Down Expand Up @@ -52,6 +60,7 @@ class BackendProxy {
}()

func login(credentials: LoginCredentials) throws -> BackenApiType {
try checkServerReachablity(credentials: credentials)
ampacheApi.authenticate(credentials: credentials)
if ampacheApi.isAuthenticated() {
selectedApi = .ampache
Expand All @@ -62,7 +71,41 @@ class BackendProxy {
selectedApi = .subsonic
return .subsonic
}
throw AuthenticationError.notAbleToLogin
throw AuthenticationError(kind: .notAbleToLogin)
}

private func checkServerReachablity(credentials: LoginCredentials) throws {
guard let serverUrl = URL(string: credentials.serverUrl) else {
throw AuthenticationError(kind: .invalidUrl)
}

let group = DispatchGroup()
group.enter()

let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let request = URLRequest(url: serverUrl)
var downloadError: AuthenticationError? = nil
let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
if let error = error {
downloadError = AuthenticationError(message: error.localizedDescription, kind: .downloadError)
} else {
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
if statusCode > 400 {
downloadError = AuthenticationError(message: "\(statusCode)", kind: .requestStatusError)
} else {
os_log("Server url is reachable. Status code: %d", log: self.log, type: .info, statusCode)
}
}
}
group.leave()
}
task.resume()
group.wait()

if let error = downloadError {
throw error
}
}

}
Expand Down
13 changes: 12 additions & 1 deletion Amperfy/Screens/ViewController/LoginVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,19 @@ class LoginVC: UIViewController {
credentials.backendApi = authenticatedApi
appDelegate.storage.saveLoginCredentials(credentials: credentials)
performSegue(withIdentifier: "toSync", sender: self)
} catch let e as AuthenticationError {
switch e.kind {
case .notAbleToLogin:
showErrorMsg(message: "Not able to login, please check credentials!")
case .invalidUrl:
showErrorMsg(message: "Server url is invalid!")
case .requestStatusError:
showErrorMsg(message: "Requesting server url finished with status response error code '\(e.message)'!")
case .downloadError:
showErrorMsg(message: e.message)
}
} catch {
showErrorMsg(message: "Not able to login, please check credentials!")
showErrorMsg(message: "Not able to login!")
}
}

Expand Down

0 comments on commit 85cb35f

Please sign in to comment.