Skip to content

Commit

Permalink
chore: add error message when querying mongodb fails
Browse files Browse the repository at this point in the history
  • Loading branch information
kmruiz committed Jan 16, 2025
1 parent 6f3d104 commit cb7b558
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.coroutines.launch
import java.lang.ref.WeakReference
import java.util.Collections
import java.util.UUID
import java.util.concurrent.CopyOnWriteArrayList

private val inspectionTelemetry = Dispatchers.IO
private val logger: Logger = logger<InspectionStatusChangedProbe>()
Expand Down Expand Up @@ -47,7 +48,7 @@ class InspectionStatusChangedProbe(
val elementsWithProblems = problemsByInspectionType(inspectionType)

// check if the element is already in the list
if (isElementRegistered(elementsWithProblems, psiElement)) {
if (isElementRegistered(elementsWithProblems) { psiElement }) {
// do nothing, it's already registered
return@launch
}
Expand Down Expand Up @@ -85,7 +86,7 @@ class InspectionStatusChangedProbe(
cs.launch(inspectionTelemetry) {
val elementsWithProblems = problemsByInspectionType(inspectionType)

if (isElementRegistered(elementsWithProblems, psiElement)) {
if (isElementRegistered(elementsWithProblems) { psiElement }) {
// do nothing, it's already registered
return@launch
}
Expand Down Expand Up @@ -124,14 +125,14 @@ class InspectionStatusChangedProbe(
// if at the end of the processing cycle it's empty
// we will assume they are
for (loopResult in results) {
for ((idx, elementWithProblem) in elementsWithProblems.withIndex()) {
if (isElementRegistered(elementsWithProblems, loopResult.psiElement)) {
for (elementWithProblem in elementsWithProblems) {
if (isElementRegistered(elementsWithProblems, loopResult::getPsiElement)) {
// the problem is still there, so don't do anything
// do nothing, it's already registered
break
}

elementsWithProblems.removeAt(idx)
elementsWithProblems.remove(elementWithProblem)

val dialect =
elementWithProblem.on.get()?.component<HasSourceDialect>() ?: continue
Expand All @@ -158,19 +159,19 @@ class InspectionStatusChangedProbe(

private suspend fun isElementRegistered(
elementsWithProblems: MutableList<UniqueInspection>,
psiElement: PsiElement
psiElement: () -> PsiElement
): Boolean = runCatching {
readAction<Boolean> {
elementsWithProblems.containsElements {
it.on.get()?.source == psiElement ||
it.on.get()?.source?.isEquivalentTo(psiElement) == true
it.on.get()?.source?.isEquivalentTo(psiElement()) == true
}
}
}.getOrDefault(false)

private fun problemsByInspectionType(inspectionType: InspectionType): MutableList<UniqueInspection> {
val result = problemsByInspectionType.computeIfAbsent(inspectionType) {
Collections.synchronizedList(mutableListOf())
CopyOnWriteArrayList()
}

result.removeAll { it.on.get() == null }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Document queryMovieById(String id) {
.first();
}

public List<Document> queryMoviesByYear(String year) {
public List<Document> queryMoviesByYear(int year) {
return client
.getDatabase("sample_mflix")
.getCollection("movies")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.intellij.database.dataSource.LocalDataSource
import com.intellij.database.dataSource.connection.ConnectionRequestor
import com.intellij.database.run.ConsoleRunConfiguration
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.mongodb.ConnectionString
import com.mongodb.MongoClientSettings
Expand Down Expand Up @@ -44,6 +46,8 @@ private const val TIMEOUT = 5
@OptIn(ExperimentalCoroutinesApi::class)
private val mongosh = Dispatchers.IO.limitedParallelism(1)

private val logger: Logger = logger<DataGripMongoDbDriver>()

/**
* The driver itself. Shouldn't be used directly, but through the
* DataGripBasedReadModelProvider.
Expand Down Expand Up @@ -206,8 +210,16 @@ internal class DataGripMongoDbDriver(

withTimeout(timeout) {
val listOfResults = mutableListOf<T>()
val resultSet = statement.executeQuery() ?: return@withTimeout emptyList()
val queryResult = runCatching { statement.executeQuery() }
if (queryResult.isFailure) {
logger.error(
"Can not query MongoDB: $queryString",
queryResult.exceptionOrNull()
)
return@withTimeout emptyList()
}

val resultSet = queryResult.getOrNull() ?: return@withTimeout emptyList()
if (resultClass.java == Unit::class.java) {
listOfResults.add(Unit as T)
return@withTimeout listOfResults
Expand Down

0 comments on commit cb7b558

Please sign in to comment.