i'm building a multi-module jetpack-compose login/signup app. My backend is handled using Spring boot and the login/signup function returns a ResponseEntity.
The task: in my frontend i want to display different messages based on type of error. Current solution: i Have a network module where i use Retrofit to call backend. This is the files in netwrok module.
interface AuthApi {
@POST("/auth/register")
suspend fun register(
@Body request: RegisterRequest,
)
@POST("/auth/login")
suspend fun login(
@Body request: LoginRequest,
): TokenPair
@GET("authenticate")
suspend fun authenticate(
@Header("Authorization") token: String,
)
}
@Module
@InstallIn(SingletonComponent::class)
internal object NetworkModule {
@Provides
@Singleton
fun provideAuthApi(
okhttpCallFactory: dagger.Lazy<Call.Factory>,
): AuthApi {
return Retrofit.Builder()
.baseUrl(SPOTIFY_BASE_URL)
.callFactory { okhttpCallFactory.get().newCall(it) }
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(AuthApi::class.java)
}
Then this is my data module.
class AuthRepositoryImpl
@Inject
constructor(
private val api: AuthApi,
) : AuthRepository {
override suspend fun signIn(email: String, password: String): Result<TokenPair, DataError.Network> {
return try {
val user = api.login(LoginRequest(email, password))
Result.Success(user)
} catch (e: HttpException) {
when (e.code()) {
400 -> Result.Error(DataError.Network.BAD_REQUEST)
401 -> Result.Error(DataError.Network.UNAUTHORIZED)
409 -> Result.Error(DataError.Network.CONFLICT)
else -> Result.Error(DataError.Network.INTERNAL_SERVER_ERROR)
}
}
}
sealed interface DataError : Error {
enum class Network : DataError {
BAD_REQUEST,
CONFLICT,
UNAUTHORIZED,
INTERNAL_SERVER_ERROR,
}
}
sealed interface Result<out D, out E : RootError> {
data class Success<out D, out E : RootError>(val data: D) : Result<D, E>
data class Error<out D, out E : RootError>(val error: E) : Result<D, E>
}
My problem is that in data layer i'm using HttpException which come from Retrofit (i add retrofit core dependency in data module too as well as network module).which means i'm not benefiting the idea of multi-module.