Skip to content

Commit 054fbed

Browse files
authored
Merge pull request #2102 from digma-ai/fix-fatal-errors
catch exceptions in settings changed Closes #2100
2 parents 785b3f8 + 3ea56b7 commit 054fbed

File tree

4 files changed

+78
-21
lines changed

4 files changed

+78
-21
lines changed

ide-common/src/main/java/org/digma/intellij/plugin/analytics/AnalyticsService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.intellij.openapi.Disposable;
5+
import com.intellij.openapi.application.ApplicationManager;
56
import com.intellij.openapi.diagnostic.*;
67
import com.intellij.openapi.project.Project;
78
import com.intellij.ui.JBColor;
@@ -152,6 +153,8 @@ private synchronized void replaceClient(String url) {
152153

153154
BackendInfoHolder.getInstance().updateInBackground();
154155

156+
ApplicationManager.getApplication().getMessageBus().syncPublisher(ApiClientChangedEvent.getAPI_CLIENT_CHANGED_TOPIC()).apiClientChanged(url);
157+
155158
}
156159

157160

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.digma.intellij.plugin.analytics
2+
3+
import com.intellij.util.messages.Topic
4+
5+
6+
/**
7+
* this is an application event that should fire when we change the api client,
8+
* usually when user changes the api url in settings.
9+
*/
10+
interface ApiClientChangedEvent {
11+
companion object {
12+
@JvmStatic
13+
@Topic.AppLevel
14+
val API_CLIENT_CHANGED_TOPIC: Topic<ApiClientChangedEvent> = Topic.create(
15+
"API CLIENT CHANGED",
16+
ApiClientChangedEvent::class.java
17+
)
18+
}
19+
20+
fun apiClientChanged(newUrl: String)
21+
22+
}

ide-common/src/main/kotlin/org/digma/intellij/plugin/updates/AggressiveUpdateService.kt

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import kotlinx.coroutines.isActive
1616
import kotlinx.coroutines.launch
1717
import org.apache.maven.artifact.versioning.ComparableVersion
1818
import org.digma.intellij.plugin.analytics.AnalyticsService
19+
import org.digma.intellij.plugin.analytics.ApiClientChangedEvent
1920
import org.digma.intellij.plugin.analytics.BackendConnectionEvent
2021
import org.digma.intellij.plugin.common.ExceptionUtils
2122
import org.digma.intellij.plugin.common.buildVersionRequest
@@ -29,7 +30,6 @@ import org.digma.intellij.plugin.model.rest.version.BackendDeploymentType
2930
import org.digma.intellij.plugin.model.rest.version.VersionResponse
3031
import org.digma.intellij.plugin.posthog.ActivityMonitor
3132
import org.digma.intellij.plugin.settings.InternalFileSettings
32-
import org.digma.intellij.plugin.settings.SettingsState
3333
import org.digma.intellij.plugin.updates.AggressiveUpdateService.Companion.getInstance
3434
import org.digma.intellij.plugin.updates.CurrentUpdateState.OK
3535
import org.digma.intellij.plugin.updates.CurrentUpdateState.UPDATE_BACKEND
@@ -112,17 +112,29 @@ class AggressiveUpdateService : Disposable {
112112
})
113113

114114

115-
SettingsState.getInstance().addChangeListener({
116-
117-
@Suppress("UnstableApiUsage")
118-
disposingScope().launch {
119-
//update state immediately after settings change. we are interested in api url change but it will
120-
// do no harm to call it on any settings change
121-
updateState()
122-
//and call startMonitoring just in case it is stopped by a previous connectionLost but there was no connection gained
123-
startMonitoring()
115+
ApplicationManager.getApplication().messageBus.connect(this).subscribe(ApiClientChangedEvent.API_CLIENT_CHANGED_TOPIC, object :
116+
ApiClientChangedEvent {
117+
override fun apiClientChanged(newUrl: String) {
118+
@Suppress("UnstableApiUsage")
119+
disposingScope().launch {
120+
try {
121+
//update state immediately after settings change. we are interested in api url change, but it will
122+
// do no harm to call it on any settings change
123+
updateState()
124+
} catch (c: CancellationException) {
125+
Log.debugWithException(logger, c, "settings change canceled {}", c)
126+
} catch (e: Throwable) {
127+
val message = ExceptionUtils.getNonEmptyMessage(e)
128+
Log.debugWithException(logger, e, "error in settings changed {}", message)
129+
errorReporter.reportError("${this::class.simpleName}.settingsChange", e)
130+
}
131+
132+
//and call startMonitoring just in case it is stopped by a previous connectionLost but there was no connection gained
133+
startMonitoring()
134+
}
124135
}
125-
}, this)
136+
137+
})
126138

127139
} else {
128140
Log.log(logger::info, "not starting, disabled in internal settings")

ide-common/src/main/kotlin/org/digma/intellij/plugin/updates/UpdatesService.kt

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import kotlinx.coroutines.isActive
1313
import kotlinx.coroutines.launch
1414
import org.apache.maven.artifact.versioning.ComparableVersion
1515
import org.digma.intellij.plugin.analytics.AnalyticsService
16+
import org.digma.intellij.plugin.analytics.ApiClientChangedEvent
1617
import org.digma.intellij.plugin.analytics.BackendConnectionEvent
1718
import org.digma.intellij.plugin.common.EDT
1819
import org.digma.intellij.plugin.common.ExceptionUtils
@@ -25,7 +26,6 @@ import org.digma.intellij.plugin.model.rest.version.BackendDeploymentType
2526
import org.digma.intellij.plugin.model.rest.version.BackendVersionResponse
2627
import org.digma.intellij.plugin.model.rest.version.VersionResponse
2728
import org.digma.intellij.plugin.settings.InternalFileSettings
28-
import org.digma.intellij.plugin.settings.SettingsState
2929
import org.digma.intellij.plugin.ui.panels.DigmaResettablePanel
3030
import java.util.concurrent.TimeUnit
3131
import kotlin.time.Duration
@@ -93,27 +93,47 @@ class UpdatesService(private val project: Project) : Disposable {
9393

9494
override fun connectionGained() {
9595
Log.log(logger::debug, "got connectionGained")
96-
//update state immediately after connectionGained, so it will not wait the delay for checking the versions.
97-
checkForNewerVersions()
96+
97+
try {
98+
//update state immediately after connectionGained, so it will not wait the delay for checking the versions.
99+
checkForNewerVersions()
100+
} catch (e: CancellationException) {
101+
Log.debugWithException(logger, e, "Exception in checkForNewerVersions")
102+
} catch (e: Throwable) {
103+
Log.debugWithException(logger, e, "Exception in checkForNewerVersions {}", ExceptionUtils.getNonEmptyMessage(e))
104+
ErrorReporter.getInstance().reportError("UpdatesService.connectionGained", e)
105+
}
98106
}
99107
})
100108

101109

102-
SettingsState.getInstance().addChangeListener({
103-
@Suppress("UnstableApiUsage")
104-
disposingScope().launch {
105-
//update state immediately after settings change. we are interested in api url change, but it will
106-
// do no harm to call it on any settings change
107-
checkForNewerVersions()
110+
111+
ApplicationManager.getApplication().messageBus.connect(this)
112+
.subscribe(ApiClientChangedEvent.API_CLIENT_CHANGED_TOPIC, object : ApiClientChangedEvent {
113+
override fun apiClientChanged(newUrl: String) {
114+
@Suppress("UnstableApiUsage")
115+
disposingScope().launch {
116+
try {
117+
//update state immediately after settings change. we are interested in api url change, but it will
118+
// do no harm to call it on any settings change
119+
checkForNewerVersions()
120+
} catch (e: CancellationException) {
121+
Log.debugWithException(logger, e, "Exception in checkForNewerVersions")
122+
} catch (e: Throwable) {
123+
Log.debugWithException(logger, e, "Exception in checkForNewerVersions {}", ExceptionUtils.getNonEmptyMessage(e))
124+
ErrorReporter.getInstance().reportError("UpdatesService.settingsChanged", e)
125+
}
126+
}
108127
}
109128

110-
}, this)
129+
})
111130
}
112131

113132
override fun dispose() {
114133
//nothing to do , used as parent disposable
115134
}
116135

136+
//this method may throw exception, always catch and report
117137
private fun checkForNewerVersions() {
118138

119139
Log.log(logger::trace, "checking for new versions")

0 commit comments

Comments
 (0)