From 68ec20c4a06718c7b692fee849bb7866d79765cc Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Tue, 31 Jan 2023 14:36:35 -0300 Subject: [PATCH 1/2] Improve JDBC driver error message in Agroal processor This improves the error message when the DB Kind is not found. ``` Unable to find a JDBC driver corresponding to the database kind 'postgres' for the default datasource (available: 'postgresql'). Check if it's a typo, otherwise provide a suitable JDBC driver extension, define the driver manually, or disable the JDBC datasource by adding 'quarkus.datasource.jdbc=false' to your configuration if you don't need it. ``` Based on feedback from https://github.com/quarkusio/quarkus/issues/30701#issuecomment-1410757424 --- .../agroal/deployment/AgroalProcessor.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java b/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java index 482f95f8cf1f5..fa4df5c2782cd 100644 --- a/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java +++ b/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java @@ -7,6 +7,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Optional; +import java.util.stream.Collectors; import javax.enterprise.inject.Default; import javax.inject.Singleton; @@ -344,14 +345,13 @@ private String resolveDriver(String dataSourceName, String dbKind, } } - throw new ConfigurationException("Unable to find a JDBC driver corresponding to the database kind '" - + dbKind + "' for the " - + (DataSourceUtil.isDefault(dataSourceName) ? "default datasource" - : "datasource '" + dataSourceName + "'") - + ". Either provide a suitable JDBC driver extension, define the driver manually, or disable the JDBC datasource by adding " - + (DataSourceUtil.isDefault(dataSourceName) ? "'quarkus.datasource.jdbc=false'" - : "'quarkus.datasource." + dataSourceName + ".jdbc=false'") - + " to your configuration if you don't need it."); + throw new ConfigurationException(String.format( + "Unable to find a JDBC driver corresponding to the database kind '%s' for the %s (available: '%s'). " + + "Check if it's a typo, otherwise provide a suitable JDBC driver extension, define the driver manually, or disable the JDBC datasource by adding %s to your configuration if you don't need it.", + dbKind, DataSourceUtil.isDefault(dataSourceName) ? "default datasource" : "datasource '" + dataSourceName + "'", + jdbcDriverBuildItems.stream().map(JdbcDriverBuildItem::getDbKind).collect(Collectors.joining("','")), + DataSourceUtil.isDefault(dataSourceName) ? "'quarkus.datasource.jdbc=false'" + : "'quarkus.datasource." + dataSourceName + ".jdbc=false'")); } @BuildStep From 8a4897e35ca1beb15d8d1be23c6cb30304309e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Wed, 1 Feb 2023 10:07:38 +0100 Subject: [PATCH 2/2] Rely on DataSourceUtil.dataSourcePropertyKey for error messages --- .../io/quarkus/agroal/deployment/AgroalProcessor.java | 6 +++--- .../java/io/quarkus/agroal/runtime/DataSources.java | 10 ++-------- .../datasource/common/runtime/DataSourceUtil.java | 8 ++++++++ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java b/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java index fa4df5c2782cd..59f72061b792d 100644 --- a/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java +++ b/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java @@ -347,11 +347,11 @@ private String resolveDriver(String dataSourceName, String dbKind, throw new ConfigurationException(String.format( "Unable to find a JDBC driver corresponding to the database kind '%s' for the %s (available: '%s'). " - + "Check if it's a typo, otherwise provide a suitable JDBC driver extension, define the driver manually, or disable the JDBC datasource by adding %s to your configuration if you don't need it.", + + "Check if it's a typo, otherwise provide a suitable JDBC driver extension, define the driver manually," + + " or disable the JDBC datasource by adding '%s=false' to your configuration if you don't need it.", dbKind, DataSourceUtil.isDefault(dataSourceName) ? "default datasource" : "datasource '" + dataSourceName + "'", jdbcDriverBuildItems.stream().map(JdbcDriverBuildItem::getDbKind).collect(Collectors.joining("','")), - DataSourceUtil.isDefault(dataSourceName) ? "'quarkus.datasource.jdbc=false'" - : "'quarkus.datasource." + dataSourceName + ".jdbc=false'")); + DataSourceUtil.dataSourcePropertyKey(dataSourceName, "jdbc"))); } @BuildStep diff --git a/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java b/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java index 28172208ff026..e0c703bc38a0c 100644 --- a/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java +++ b/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java @@ -138,16 +138,10 @@ public AgroalDataSource doCreateDataSource(String dataSourceName) { DataSourceSupport.Entry matchingSupportEntry = dataSourceSupport.entries.get(dataSourceName); if (!dataSourceJdbcRuntimeConfig.url.isPresent()) { - String errorMessage; - // we don't have any URL configuration so using a standard message - if (DataSourceUtil.isDefault(dataSourceName)) { - errorMessage = "quarkus.datasource.jdbc.url has not been defined"; - } else { - errorMessage = "quarkus.datasource." + dataSourceName + ".jdbc.url has not been defined"; - } //this is not an error situation, because we want to allow the situation where a JDBC extension //is installed but has not been configured - return new UnconfiguredDataSource(errorMessage); + return new UnconfiguredDataSource( + DataSourceUtil.dataSourcePropertyKey(dataSourceName, "jdbc.url") + " has not been defined"); } // we first make sure that all available JDBC drivers are loaded in the current TCCL diff --git a/extensions/datasource/common/src/main/java/io/quarkus/datasource/common/runtime/DataSourceUtil.java b/extensions/datasource/common/src/main/java/io/quarkus/datasource/common/runtime/DataSourceUtil.java index 7fdf31b27cf9e..f0a4b3378f1ba 100644 --- a/extensions/datasource/common/src/main/java/io/quarkus/datasource/common/runtime/DataSourceUtil.java +++ b/extensions/datasource/common/src/main/java/io/quarkus/datasource/common/runtime/DataSourceUtil.java @@ -15,6 +15,14 @@ public static boolean hasDefault(Collection dataSourceNames) { return dataSourceNames.contains(DEFAULT_DATASOURCE_NAME); } + public static String dataSourcePropertyKey(String datasourceName, String radical) { + if (datasourceName == null || DataSourceUtil.isDefault(datasourceName)) { + return "quarkus.datasource." + radical; + } else { + return "quarkus.datasource.\"" + datasourceName + "\"." + radical; + } + } + public static List dataSourcePropertyKeys(String datasourceName, String radical) { if (datasourceName == null || DataSourceUtil.isDefault(datasourceName)) { return List.of("quarkus.datasource." + radical);