From 7b07d54398552980a6eb42305b6fb9dab1094a59 Mon Sep 17 00:00:00 2001 From: Dmitry Barashev Date: Fri, 27 Sep 2024 21:39:24 +0400 Subject: [PATCH] Better SQL error handling Update issue #2514 --- biz.ganttproject.app.localization | 2 +- .../customproperty/CalculationMethod.kt | 5 +++-- .../ganttview/SharedUiComponents.kt | 6 +----- .../ganttproject/storage/ProjectDatabase.kt | 21 +++++++++++++++++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/biz.ganttproject.app.localization b/biz.ganttproject.app.localization index d313bae516..3935c6ef49 160000 --- a/biz.ganttproject.app.localization +++ b/biz.ganttproject.app.localization @@ -1 +1 @@ -Subproject commit d313bae51664105f2df7adfaa48d8fb907720764 +Subproject commit 3935c6ef49acc94490b6bd9e9254eb786e442b44 diff --git a/ganttproject/src/main/java/biz/ganttproject/customproperty/CalculationMethod.kt b/ganttproject/src/main/java/biz/ganttproject/customproperty/CalculationMethod.kt index c5f898fe43..7fc3627c49 100644 --- a/ganttproject/src/main/java/biz/ganttproject/customproperty/CalculationMethod.kt +++ b/ganttproject/src/main/java/biz/ganttproject/customproperty/CalculationMethod.kt @@ -49,8 +49,9 @@ class CalculationMethodValidator(private val projectDatabase: ProjectDatabase) { try { projectDatabase.validateColumnConsumer(ColumnConsumer(calculationMethod) {_,_->}) } catch (ex: ProjectDatabaseException) { - //GPLogger.create("ProjectDatabase").error("calculation method validation failed: ${ex.message}", ex) - throw ValidationException(RootLocalizer.formatText("option.customPropertyDialog.expression.validation.syntax")) + //GPLogger.create("ProjectDatabase").error("calculation method validation failed: ${ex.reason}", exception = ex) + //ex.printStackTrace(System.out) + throw ValidationException(RootLocalizer.formatText("option.customPropertyDialog.expression.validation.syntax", ex.reason)) } } } diff --git a/ganttproject/src/main/java/biz/ganttproject/ganttview/SharedUiComponents.kt b/ganttproject/src/main/java/biz/ganttproject/ganttview/SharedUiComponents.kt index 9ed6d25e33..5903fb4d20 100644 --- a/ganttproject/src/main/java/biz/ganttproject/ganttview/SharedUiComponents.kt +++ b/ganttproject/src/main/java/biz/ganttproject/ganttview/SharedUiComponents.kt @@ -149,6 +149,7 @@ internal open class ItemEditorPane>( internal val propertySheet = PropertySheetBuilder(localizer).createPropertySheet(fields) private val errorLabel = Label().also { it.styleClass.addAll("hint", "hint-validation") + it.isWrapText = true } private val errorPane = HBox().also { it.styleClass.addAll("hint-validation-pane", "noerror") @@ -291,11 +292,6 @@ internal class ItemListDialogPane>( internal val listView: ListView = ListView() init { -// selectedItem.addWatcher { evt -> -// if (evt.trigger != listView && evt.newValue != null) { -// listItems.replaceAll { if (it == evt.newValue?.cloneOf) { evt.newValue?.clone(forEditing = false) } else { it } } -// } -// } listView.apply { this@ItemListDialogPane.dialogModel.selection = { selectionModel.selectedItems } items = listItems diff --git a/ganttproject/src/main/java/net/sourceforge/ganttproject/storage/ProjectDatabase.kt b/ganttproject/src/main/java/net/sourceforge/ganttproject/storage/ProjectDatabase.kt index 5900645669..994e9b6655 100644 --- a/ganttproject/src/main/java/net/sourceforge/ganttproject/storage/ProjectDatabase.kt +++ b/ganttproject/src/main/java/net/sourceforge/ganttproject/storage/ProjectDatabase.kt @@ -28,11 +28,32 @@ import biz.ganttproject.customproperty.SimpleSelect import biz.ganttproject.storage.db.tables.records.TaskRecord import net.sourceforge.ganttproject.task.Task import net.sourceforge.ganttproject.task.dependency.TaskDependency +import org.h2.jdbc.JdbcException import java.awt.Color +private val SYNTAX_ERROR_PREFIX = """Syntax error in SQL statement """" open class ProjectDatabaseException: Exception { constructor(message: String): super(message) constructor(message: String, cause: Throwable): super(message, cause) + + val reason: String get() = + this.cause?.let { + if (it.cause is JdbcException) { + var message = (it.cause as Throwable).message!! + val hasSqlStatement = message.indexOf("; SQL statement:") + if (hasSqlStatement != -1) { + return message.substring(0 until hasSqlStatement) + } + if (message.startsWith(SYNTAX_ERROR_PREFIX)) { + val posMarker = message.indexOf("[*]") + if (posMarker != -1) { + return message.substring(SYNTAX_ERROR_PREFIX.length until posMarker) + } + } + message + } else null + } ?: this.message ?: "" + } interface ProjectDatabaseTxn {