diff --git a/src/main/xerus/monstercat/api/APIConnection.kt b/src/main/xerus/monstercat/api/APIConnection.kt index 1a6e505..e6916b9 100644 --- a/src/main/xerus/monstercat/api/APIConnection.kt +++ b/src/main/xerus/monstercat/api/APIConnection.kt @@ -33,6 +33,7 @@ import java.io.IOException import java.io.InputStream import java.lang.ref.WeakReference import java.net.URI +import java.net.UnknownHostException import kotlin.math.min import kotlin.reflect.KClass @@ -226,20 +227,33 @@ class APIConnection(vararg path: String): HTTPQuery() { return ConnectResult(connectsid, validity, session) } - fun login(username: String, password: String): Boolean { + enum class LoginStatus{ + CONNECTED, + INVALID, + NOINTERNET, + UNKNOWNERROR + } + + fun login(username: String, password: String): LoginStatus { val connection = APIConnection("v2", "signin") val context = HttpClientContext() - connection.execute(HttpPost(connection.uri).apply { - setHeader("Accept", "application/json") - setHeader("Content-type", "application/json") - entity = StringEntity("""{"email":"$username","password":"$password"}""") - }, context) + try { + connection.execute(HttpPost(connection.uri).apply { + setHeader("Accept", "application/json") + setHeader("Content-type", "application/json") + entity = StringEntity("""{"email":"$username","password":"$password"}""") + }, context) + } catch(e: Exception) { + logger.warn("Error when trying to login through API", e) + return if(e is UnknownHostException) LoginStatus.NOINTERNET + else LoginStatus.UNKNOWNERROR + } val code = connection.response?.statusLine?.statusCode logger.trace("Login POST returned response code $code") - if (code !in 200..206) return false - CONNECTSID.value = (context.cookieStore.cookies.find { it.name == "connect.sid" }?.value ?: return false) - return true + if (code !in 200..206) return LoginStatus.INVALID + CONNECTSID.value = (context.cookieStore.cookies.find { it.name == "connect.sid" }?.value ?: return LoginStatus.INVALID) + return LoginStatus.CONNECTED } fun logout() { diff --git a/src/main/xerus/monstercat/downloader/TabDownloader.kt b/src/main/xerus/monstercat/downloader/TabDownloader.kt index 8ffee54..0739d92 100644 --- a/src/main/xerus/monstercat/downloader/TabDownloader.kt +++ b/src/main/xerus/monstercat/downloader/TabDownloader.kt @@ -36,6 +36,7 @@ import xerus.ktutil.javafx.ui.controls.alwaysTruePredicate import xerus.ktutil.javafx.ui.initWindowOwner import xerus.ktutil.preferences.PropertySetting import xerus.monstercat.api.APIConnection +import xerus.monstercat.api.APIConnection.Companion.LoginStatus.* import xerus.monstercat.api.ConnectValidity import xerus.monstercat.api.Covers import xerus.monstercat.api.response.ArtistRel @@ -419,7 +420,7 @@ class TabDownloader: VTab() { val emailField = TextField("").apply { promptText = "Email address" } val passwordField = PasswordField().apply { promptText = "Password" } - val errorMessage = Label("Wrong username / password").apply { isVisible = false; managedProperty().bind(visibleProperty()) } + val errorMessage = Label("An error has been encountered").apply { isVisible = false; managedProperty().bind(visibleProperty()) } val loadingGif = ImageView(Image("img/loading-16.gif")).apply { isVisible = false } val buttonHBox = HBox() @@ -437,15 +438,20 @@ class TabDownloader: VTab() { val login = APIConnection.login(emailField.text, passwordField.text) logger.debug("Login as ${emailField.text} returned $login") onFx { - if(login) { - stage.close() - } else { + fun showErrorText(message: String) { + errorMessage.text = message errorMessage.isVisible = true loadingGif.isVisible = false buttonHBox.isDisable = false - stage.sizeToScene() } + + when(login) { + CONNECTED -> stage.close() + INVALID -> showErrorText("Wrong username / password") + NOINTERNET -> showErrorText("You are not connected to the Internet") + UNKNOWNERROR -> showErrorText("An error has occured.") + } } } }