fix: Don't capture all errors on failed login/register

Only capture the relevant errors that we know how to handle, propagate
the rest up instead of trying to catch & handle them gracefully. This is
important, as our global exception handler might have a way to handle
these errors (namely the connection error, moving the user to the api
unreachable screen).
This commit is contained in:
Peter Vacho 2025-01-05 21:43:13 +01:00
parent 9aa0fbc12b
commit 5a8d882000
Signed by: school
GPG key ID: 8CFC3837052871B4
4 changed files with 12 additions and 20 deletions

View file

@ -78,10 +78,10 @@ class LoginActivity : AppCompatActivity() {
.show() .show()
} }
is LoginResult.UnknownError -> { is LoginResult.BadCredentials -> {
Toast.makeText( Toast.makeText(
this@LoginActivity, this@LoginActivity,
getString(R.string.login_failed_please_try_again), getString(R.string.login_credentials_invalid),
Toast.LENGTH_LONG Toast.LENGTH_LONG
).show() ).show()
} }

View file

@ -86,10 +86,6 @@ class RegisterActivity : AppCompatActivity() {
Toast.makeText(this@RegisterActivity, Toast.makeText(this@RegisterActivity,
getString(R.string.user_already_exists_please_log_in), Toast.LENGTH_LONG).show() getString(R.string.user_already_exists_please_log_in), Toast.LENGTH_LONG).show()
} }
is RegisterResult.UnknownError -> {
Toast.makeText(this@RegisterActivity,
getString(R.string.registration_failed_try_again_later), Toast.LENGTH_LONG).show()
}
} }
} }
} }

View file

@ -15,13 +15,12 @@ sealed class RegisterResult {
data class Success(val response: RegisterResponse) : RegisterResult() data class Success(val response: RegisterResponse) : RegisterResult()
data class ValidationError(val errorData: com.p_vacho.neat_calendar.api.models.ValidationError) : RegisterResult() data class ValidationError(val errorData: com.p_vacho.neat_calendar.api.models.ValidationError) : RegisterResult()
data object UserAlreadyExists : RegisterResult() data object UserAlreadyExists : RegisterResult()
data class UnknownError(val exception: Throwable) : RegisterResult()
} }
sealed class LoginResult { sealed class LoginResult {
data object Success : LoginResult() data object Success : LoginResult()
data class ValidationError(val errorData: com.p_vacho.neat_calendar.api.models.ValidationError) : LoginResult() data class ValidationError(val errorData: com.p_vacho.neat_calendar.api.models.ValidationError) : LoginResult()
data class UnknownError(val exception: Throwable) : LoginResult() data object BadCredentials: LoginResult()
} }
@ -44,14 +43,15 @@ class AuthRepository(private val tokenManager: TokenManager) {
Log.w("API", "Validation error: $validationError") Log.w("API", "Validation error: $validationError")
LoginResult.ValidationError(validationError) LoginResult.ValidationError(validationError)
} }
else -> { 401 -> {
Log.w("API", "Login request failed (${e.message()})", e) Log.w("API", "Login request failed (${e.message()})", e)
LoginResult.UnknownError(e) LoginResult.BadCredentials
}
else -> {
Log.e("API", "Login request failed (${e.message()})", e)
throw e
} }
} }
} catch (e: Exception) {
Log.e("API", "Unexpected error during login", e)
LoginResult.UnknownError(e)
} }
} }
} }
@ -76,13 +76,10 @@ class AuthRepository(private val tokenManager: TokenManager) {
RegisterResult.UserAlreadyExists RegisterResult.UserAlreadyExists
} }
else -> { else -> {
Log.w("API", "Register request failed (${e.message()})", e) Log.e("API", "Register request failed (${e.message()})", e)
RegisterResult.UnknownError(e) throw e
} }
} }
} catch (e: Exception) {
Log.e("API", "Unexpected error during registration", e)
RegisterResult.UnknownError(e)
} }
} }
} }

View file

@ -62,10 +62,8 @@
<string name="user_registered_you_may_now_log_in">User registered, you may now log in</string> <string name="user_registered_you_may_now_log_in">User registered, you may now log in</string>
<string name="validation_errors">Validation Errors</string> <string name="validation_errors">Validation Errors</string>
<string name="user_already_exists_please_log_in">User already exists. Please log in.</string> <string name="user_already_exists_please_log_in">User already exists. Please log in.</string>
<string name="registration_failed_try_again_later">Registration failed. Try again later.</string>
<string name="please_fill_in_all_fields">Please fill in all fields</string> <string name="please_fill_in_all_fields">Please fill in all fields</string>
<string name="ok">OK</string> <string name="ok">OK</string>
<string name="login_failed_please_try_again">Login failed. Please try again.</string>
<string name="end_time_must_be_after_start_time">End time must be after start time</string> <string name="end_time_must_be_after_start_time">End time must be after start time</string>
<string name="user_not_authenticated_unable_to_fetch_categories">User not authenticated. Unable to fetch categories.</string> <string name="user_not_authenticated_unable_to_fetch_categories">User not authenticated. Unable to fetch categories.</string>
<string name="no_more_categories_to_add">No more categories to add</string> <string name="no_more_categories_to_add">No more categories to add</string>
@ -101,4 +99,5 @@
<string name="please_enter_a_category_name">Please enter a category name</string> <string name="please_enter_a_category_name">Please enter a category name</string>
<string name="failed_to_save_category">Failed to save category: %1$s</string> <string name="failed_to_save_category">Failed to save category: %1$s</string>
<string name="instant_event_time">At: %1$s</string> <string name="instant_event_time">At: %1$s</string>
<string name="login_credentials_invalid">Login credentials invalid</string>
</resources> </resources>