'Retrieving a plain text error message from a call response

I am building out the login form for a client, and their backend, upon successfully authenticating the user, returns the user validation data in a JSONObject as you can see below. Upon failure, however (i.e. 404 username not found), they return a plain text with the error message. I cannot figure out how to retrieve that error message when the 404 error occurs. That is also the case for many other endpoints of theirs.

In the repositoryImpl:

override suspend fun validateUser(
        username: String,
        password: String
    ): ResponseResult<UserValidationDetails> =
        withContext(Dispatchers.IO) {
            try {
                val response = authenticationControllerAPIService.validateUser(username, password)
                if (response.isSuccessful) {
                    return@withContext ResponseResult.Success(response.body())
                } else { // this is triggered when the error occurs and i need to get the msg
                    return@withContext ResponseResult.Error(Exception(response.message()))
                }
            } catch (e: Exception) {
                return@withContext ResponseResult.Error(e)
            }
        }

UserValidationDetails:

data class UserValidationDetails(
    @SerializedName("Username")
    val username: String? = null,
    @SerializedName("Password")
    val password: String? = null,
    @SerializedName("UserId")
    val userId: Long? = null,
    @SerializedName("UserLoginId")
    val userLoginId: String? = null
)

error response

successful response



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source