Simplify core error handling paths

https://github.com/2dust/v2rayNG/pull/6025
This commit is contained in:
2dust committed 2026-08-07 16:24:20 +08:00
1 parent 775546ce40
commit fd48dcf9e0
6 files changed
+19 -70

No files matched your search

@@ -5,7 +5,6 @@ import android.text.TextUtils
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.v2ray.ang.AppConfig
import com.v2ray.ang.R
import com.v2ray.ang.dto.ConfigResult
import com.v2ray.ang.dto.CoreConfigContext
import com.v2ray.ang.dto.V2rayConfig
@@ -38,8 +37,7 @@ object CoreConfigManager {
?: return ConfigResult(
status = false,
guid = guid,
errorMessage = "Failed to build config context",
displayMessage = context.getString(R.string.core_error_build_config_context)
errorMessage = "Failed to build config context"
)
if (configContext.isCustom) {
return buildV2rayCustomConfig(configContext)
@@ -47,15 +45,10 @@ object CoreConfigManager {
return toConfigResult(configContext, buildUnifiedConfig(configContext))
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Failed to get V2ray config", e)
val detail = e.message ?: e.javaClass.simpleName
return ConfigResult(
status = false,
guid = guid,
errorMessage = "Failed to get V2ray config: $detail",
displayMessage = context.getString(
R.string.core_error_get_config_detail,
detail
)
errorMessage = "Failed to get V2ray config: ${e.message ?: e.javaClass.simpleName}"
)
}
}
@@ -71,8 +64,7 @@ object CoreConfigManager {
?: return ConfigResult(
status = false,
guid = guid,
errorMessage = "Failed to build config context",
displayMessage = context.getString(R.string.core_error_build_config_context)
errorMessage = "Failed to build config context"
)
if (configContext.isCustom) {
return buildV2rayCustomConfig(configContext)
@@ -83,15 +75,10 @@ object CoreConfigManager {
return toConfigResult(configContext, v2rayConfig)
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Failed to get V2ray config for speedtest", e)
val detail = e.message ?: e.javaClass.simpleName
return ConfigResult(
status = false,
guid = guid,
errorMessage = "Failed to get V2ray config for speedtest: $detail",
displayMessage = context.getString(
R.string.core_error_get_speedtest_config_detail,
detail
)
errorMessage = "Failed to get V2ray config: ${e.message ?: e.javaClass.simpleName}"
)
}
}
@@ -105,8 +92,7 @@ object CoreConfigManager {
?: return ConfigResult(
status = false,
guid = configContext.guid,
errorMessage = "Custom config is empty",
displayMessage = context.getString(R.string.core_error_custom_config_empty)
errorMessage = "Failed to build config context, config is empty"
)
val result = ConfigResult(true, configContext.guid, raw)
@@ -58,12 +58,12 @@ object CoreNativeManager {
*
* @return Version string of the V2Ray core
*/
fun getLibVersion(): String? {
fun getLibVersion(): String {
return try {
Libv2ray.checkVersionX()
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Failed to check V2Ray version", e)
null
"Unknown"
}
}
@@ -53,11 +53,6 @@ object CoreServiceManager {
/** Tun descriptor the core was started with, null in the proxy only and root run modes. */
private var currentVpnInterface: ParcelFileDescriptor? = null
private class CoreStartException(
logMessage: String,
val displayMessage: String,
) : IllegalStateException(logMessage)
var serviceControl: SoftReference<ServiceControl>? = null
set(value) {
field = value
@@ -101,22 +96,15 @@ object CoreServiceManager {
try {
doStartCoreLoop(service, vpnInterface)
return true
} catch (e: CoreStartException) {
reportCoreStartFailure(service, e.message.orEmpty(), e.displayMessage, e)
return false
} catch (e: Exception) {
val message = e.message?.takeUnless { it.isBlank() } ?: e.javaClass.simpleName
reportCoreStartFailure(service, message, message, e)
LogUtil.e(AppConfig.TAG, "StartCore-Manager: $message", e)
MessageHelper.sendMsg2UI(service, AppConfig.MSG_STATE_START_FAILURE, message)
NotificationManager.cancelNotification()
return false
}
}
private fun reportCoreStartFailure(service: Service, logMessage: String, displayMessage: String, error: Throwable) {
LogUtil.e(AppConfig.TAG, "StartCore-Manager: $logMessage", error)
MessageHelper.sendMsg2UI(service, AppConfig.MSG_STATE_START_FAILURE, displayMessage)
NotificationManager.cancelNotification()
}
@Throws(Exception::class)
private fun doStartCoreLoop(service: Service, vpnInterface: ParcelFileDescriptor?) {
val mFilter = IntentFilter(AppConfig.BROADCAST_ACTION_SERVICE)
@@ -132,27 +120,14 @@ object CoreServiceManager {
@Throws(Exception::class)
private fun launchCore(service: Service, vpnInterface: ParcelFileDescriptor?, isReload: Boolean = false) {
val guid = MmkvManager.getSelectServer()
?: throw CoreStartException(
logMessage = "No server selected",
displayMessage = service.getString(R.string.core_error_no_server_selected)
)
val config = MmkvManager.decodeServerConfig(guid)
?: throw CoreStartException(
logMessage = "Failed to decode server config",
displayMessage = service.getString(R.string.core_error_decode_server_config)
)
val guid = MmkvManager.getSelectServer() ?: error("No server selected")
val config = MmkvManager.decodeServerConfig(guid) ?: error("Failed to decode server config")
LogUtil.i(AppConfig.TAG, "StartCore-Manager: Starting core loop for ${config.remarks}")
val result = CoreConfigManager.getV2rayConfig(service, guid)
LogUtil.d(AppConfig.TAG, result.content)
if (!result.status) {
throw CoreStartException(
logMessage = result.errorMessage.ifBlank { "Failed to get V2Ray config" },
displayMessage = result.displayMessage.ifBlank {
service.getString(R.string.core_error_get_config)
}
)
error(result.errorMessage.ifBlank { "Failed to get V2Ray config" })
}
currentConfig = config
@@ -174,10 +149,7 @@ object CoreServiceManager {
coreController.startLoop(result.content, tunFd)
if (!isRunning()) {
throw CoreStartException(
logMessage = "Core failed to start",
displayMessage = service.getString(R.string.core_error_start)
)
error("Core failed to start")
}
if (browserDialer != null) {
@@ -288,10 +260,6 @@ object CoreServiceManager {
LogUtil.i(AppConfig.TAG, "StartCore-Manager: Core reload finished")
true
} catch (e: CoreStartException) {
LogUtil.e(AppConfig.TAG, "StartCore-Manager: Failed to reload core: ${e.message}", e)
MessageHelper.sendMsg2UI(service, AppConfig.MSG_STATE_START_FAILURE, e.displayMessage)
false
} catch (e: Exception) {
val message = e.message?.takeUnless { it.isBlank() } ?: e.javaClass.simpleName
LogUtil.e(AppConfig.TAG, "StartCore-Manager: Failed to reload core: $message", e)
@@ -353,16 +321,14 @@ object CoreServiceManager {
time = coreController.measureDelay(SettingsManager.getDelayTestUrl())
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "StartCore-Manager: Failed to measure delay", e)
errorStr = e.message?.substringAfter("\":")
?: service.getString(R.string.connection_test_empty_message)
errorStr = e.message?.substringAfter("\":") ?: "empty message"
}
if (time == -1L) {
try {
time = coreController.measureDelay(SettingsManager.getDelayTestUrl(true))
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "StartCore-Manager: Failed to measure delay", e)
errorStr = e.message?.substringAfter("\":")
?: service.getString(R.string.connection_test_empty_message)
errorStr = e.message?.substringAfter("\":") ?: "empty message"
}
}
@@ -526,4 +492,4 @@ object CoreServiceManager {
}
}
}
}
}
@@ -4,9 +4,6 @@ data class ConfigResult(
var status: Boolean,
var guid: String? = null,
var content: String = "",
/** Stable English diagnostic text suitable for Logcat. */
var errorMessage: String = "",
/** Localized message suitable for the UI. */
var displayMessage: String = "",
)
@@ -53,7 +53,7 @@ fun AboutScreen(onBackClick: () -> Unit) {
val context = LocalContext.current
var showOssDialog by remember { mutableStateOf(false) }
val libVersion = CoreNativeManager.getLibVersion() ?: stringResource(R.string.value_unknown)
val libVersion = CoreNativeManager.getLibVersion()
val versionText = "v${BuildConfig.VERSION_NAME} ($libVersion)"
val appIdText = BuildConfig.APPLICATION_ID
@@ -59,7 +59,7 @@ fun CheckUpdateScreen(
val showUpdateDialog by viewModel.showUpdateDialog.collectAsStateWithLifecycle()
val updateResult by viewModel.updateResult.collectAsStateWithLifecycle()
val libVersion = CoreNativeManager.getLibVersion() ?: stringResource(R.string.value_unknown)
val libVersion = CoreNativeManager.getLibVersion()
val versionText = "v${BuildConfig.VERSION_NAME} ($libVersion)"
Scaffold(