Skip to content

Commit

Permalink
Wrap missing external calls in either catch
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Jul 24, 2024
1 parent db67d44 commit 47f8365
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import net.mullvad.mullvadvpn.lib.model.ApiAccessMethodId
import net.mullvad.mullvadvpn.lib.model.ApiAccessMethodSetting
import net.mullvad.mullvadvpn.lib.model.AppId
import net.mullvad.mullvadvpn.lib.model.AppVersionInfo as ModelAppVersionInfo
import net.mullvad.mullvadvpn.lib.model.ClearAccountHistoryError
import net.mullvad.mullvadvpn.lib.model.ClearAllOverridesError
import net.mullvad.mullvadvpn.lib.model.ConnectError
import net.mullvad.mullvadvpn.lib.model.Constraint
Expand All @@ -74,6 +75,7 @@ import net.mullvad.mullvadvpn.lib.model.GetAccountHistoryError
import net.mullvad.mullvadvpn.lib.model.GetDeviceListError
import net.mullvad.mullvadvpn.lib.model.GetDeviceStateError
import net.mullvad.mullvadvpn.lib.model.LoginAccountError
import net.mullvad.mullvadvpn.lib.model.LogoutAccountError
import net.mullvad.mullvadvpn.lib.model.NewAccessMethodSetting
import net.mullvad.mullvadvpn.lib.model.ObfuscationSettings
import net.mullvad.mullvadvpn.lib.model.Ownership as ModelOwnership
Expand Down Expand Up @@ -263,9 +265,13 @@ class ManagementService(
Either.catch { grpc.connectTunnel(Empty.getDefaultInstance()).value }
.mapLeft(ConnectError::Unknown)

suspend fun disconnect(): Boolean = grpc.disconnectTunnel(Empty.getDefaultInstance()).value
suspend fun disconnect(): Either<ConnectError, Boolean> =
Either.catch { grpc.disconnectTunnel(Empty.getDefaultInstance()).value }
.mapLeft(ConnectError::Unknown)

suspend fun reconnect(): Boolean = grpc.reconnectTunnel(Empty.getDefaultInstance()).value
suspend fun reconnect(): Either<ConnectError, Boolean> =
Either.catch { grpc.reconnectTunnel(Empty.getDefaultInstance()).value }
.mapLeft(ConnectError::Unknown)

private suspend fun getTunnelState(): ModelTunnelState =
grpc.getTunnelState(Empty.getDefaultInstance()).toDomain()
Expand All @@ -282,9 +288,13 @@ class ManagementService(
private suspend fun getVersionInfo(): ModelAppVersionInfo =
grpc.getVersionInfo(Empty.getDefaultInstance()).toDomain()

suspend fun logoutAccount() {
grpc.logoutAccount(Empty.getDefaultInstance())
}
private suspend fun getCurrentApiAccessMethod(): ApiAccessMethodSetting =
grpc.getCurrentApiAccessMethod(Empty.getDefaultInstance()).toDomain()

suspend fun logoutAccount(): Either<LogoutAccountError, Unit> =
Either.catch { grpc.logoutAccount(Empty.getDefaultInstance()) }
.mapLeft(LogoutAccountError::Unknown)
.mapEmpty()

suspend fun loginAccount(accountNumber: AccountNumber): Either<LoginAccountError, Unit> =
Either.catch { grpc.loginAccount(StringValue.of(accountNumber.value)) }
Expand All @@ -299,9 +309,10 @@ class ManagementService(
}
.mapEmpty()

suspend fun clearAccountHistory() {
grpc.clearAccountHistory(Empty.getDefaultInstance())
}
suspend fun clearAccountHistory(): Either<ClearAccountHistoryError, Unit> =
Either.catch { grpc.clearAccountHistory(Empty.getDefaultInstance()) }
.mapLeft(ClearAccountHistoryError::Unknown)
.mapEmpty()

suspend fun getAccountHistory(): Either<GetAccountHistoryError, AccountNumber?> =
Either.catch {
Expand Down Expand Up @@ -408,11 +419,6 @@ class ManagementService(
.mapLeft(SetWireguardQuantumResistantError::Unknown)
.mapEmpty()

// Todo needs to be more advanced
suspend fun setRelaySettings(value: RelaySettings) {
grpc.setRelaySettings(value.fromDomain())
}

suspend fun setObfuscation(
value: SelectedObfuscation
): Either<SetObfuscationOptionsError, Unit> =
Expand Down Expand Up @@ -626,9 +632,6 @@ class ManagementService(
.mapLeft(::UnknownApiAccessMethodError)
.mapEmpty()

private suspend fun getCurrentApiAccessMethod(): ApiAccessMethodSetting =
grpc.getCurrentApiAccessMethod(Empty.getDefaultInstance()).toDomain()

suspend fun testCustomApiAccessMethod(
customProxy: ApiAccessMethod.CustomProxy
): Either<TestApiAccessMethodError, Unit> =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.mullvad.mullvadvpn.lib.model

interface ClearAccountHistoryError {
data class Unknown(val t: Throwable) : ClearAccountHistoryError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.mullvad.mullvadvpn.lib.model

interface LogoutAccountError {
data class Unknown(val t: Throwable) : LogoutAccountError
}

0 comments on commit 47f8365

Please sign in to comment.