chore: Handle validation errors from event creation

This commit is contained in:
Peter Vacho 2025-01-01 19:30:38 +01:00
parent e8f7bf372a
commit f20ee98c39
Signed by: school
GPG key ID: 8CFC3837052871B4
3 changed files with 15 additions and 5 deletions

View file

@ -6,6 +6,7 @@ import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.Toast
@ -19,10 +20,12 @@ import com.github.dhaval2404.colorpicker.ColorPickerDialog
import com.google.android.material.button.MaterialButton
import com.google.android.material.button.MaterialButtonToggleGroup
import com.google.android.material.textfield.TextInputEditText
import com.google.gson.Gson
import com.p_vacho.neat_calendar.R
import com.p_vacho.neat_calendar.api.RetrofitClient
import com.p_vacho.neat_calendar.api.models.EventRequest
import com.p_vacho.neat_calendar.api.models.EventResponse
import com.p_vacho.neat_calendar.api.models.ValidationError
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@ -152,14 +155,20 @@ class CreateEventActivity : AppCompatActivity() {
val createdEvent = RetrofitClient.eventsService.createEvent(eventRequest)
withContext(Dispatchers.Main) { handleEventCreated(createdEvent) }
} catch (e: HttpException) {
if (e.code() != 400) {
if (e.code() != 422) {
throw e
}
val errorBody = e.response()?.errorBody()?.string()
val validationError = Gson().fromJson(errorBody, ValidationError::class.java)
val errMsg = validationError.detail.joinToString("\n")
Log.e("EventCreate", "Got HTTP 422: $validationError")
withContext(Dispatchers.Main) {
Toast.makeText(
this@CreateEventActivity,
"Failed to create event",
"Failed to create event: $errMsg",
Toast.LENGTH_SHORT
).show()
}

View file

@ -1,4 +1,4 @@
package com.p_vacho.neat_calendar.util.auth
package com.p_vacho.neat_calendar.api.models
/**
* Error data from HTTP 422 (Unprocessable entity) responses.

View file

@ -9,17 +9,18 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import retrofit2.HttpException
import com.google.gson.Gson
import com.p_vacho.neat_calendar.api.models.ValidationError
sealed class RegisterResult {
data class Success(val response: RegisterResponse) : RegisterResult()
data class ValidationError(val errorData: com.p_vacho.neat_calendar.util.auth.ValidationError) : RegisterResult()
data class ValidationError(val errorData: com.p_vacho.neat_calendar.api.models.ValidationError) : RegisterResult()
data object UserAlreadyExists : RegisterResult()
data class UnknownError(val exception: Throwable) : RegisterResult()
}
sealed class LoginResult {
data object Success : LoginResult()
data class ValidationError(val errorData: com.p_vacho.neat_calendar.util.auth.ValidationError) : LoginResult()
data class ValidationError(val errorData: com.p_vacho.neat_calendar.api.models.ValidationError) : LoginResult()
data class UnknownError(val exception: Throwable) : LoginResult()
}