diff --git a/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java b/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java
index 61c560ebdf..7855db1520 100644
--- a/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java
+++ b/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java
@@ -482,7 +482,9 @@ protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
xmlConfigBuilder.parse();
LOGGER.debug(() -> "Parsed configuration file: '" + this.configLocation + "'");
} catch (Exception ex) {
- throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
+ throw new NestedIOException(
+ "Failed to parse config resource: " + this.configLocation + " error detail: " + ErrorContext.instance(),
+ ex);
} finally {
ErrorContext.instance().reset();
}
@@ -505,7 +507,9 @@ protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
configuration, mapperLocation.toString(), configuration.getSqlFragments());
xmlMapperBuilder.parse();
} catch (Exception e) {
- throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
+ throw new NestedIOException(
+ "Failed to parse mapping resource: '" + mapperLocation + "'" + " error detail: " + ErrorContext
+ .instance(), e);
} finally {
ErrorContext.instance().reset();
}
diff --git a/src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java b/src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java
index 6f32911faf..4a5aac85e6 100644
--- a/src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java
+++ b/src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java
@@ -295,6 +295,16 @@ void testFragmentsAreReadWithMapperLocations() throws Exception {
assertThat(factory.getConfiguration().getSqlFragments().size()).isEqualTo(2);
}
+ @Test
+ void testErrorMessageWhenXmlConfigBuilderParseError() throws Exception {
+ setupFactoryBean();
+
+ factoryBean.setConfigLocation(new org.springframework.core.io.ClassPathResource(
+ "org/mybatis/spring/mybatis-config-problem.xml"));
+ Throwable e = assertThrows(Exception.class, () -> factoryBean.getObject());
+ assertThat(e.getMessage()).contains("The error may exist in org/mybatis/spring/TestProblemMapper.xml");
+ }
+
@Test
void testNullMapperLocations() throws Exception {
setupFactoryBean();
diff --git a/src/test/java/org/mybatis/spring/TestProblemMapper.xml b/src/test/java/org/mybatis/spring/TestProblemMapper.xml
new file mode 100644
index 0000000000..a7f7145dac
--- /dev/null
+++ b/src/test/java/org/mybatis/spring/TestProblemMapper.xml
@@ -0,0 +1,28 @@
+
+
+
+
+
+
+
diff --git a/src/test/java/org/mybatis/spring/mybatis-config-problem.xml b/src/test/java/org/mybatis/spring/mybatis-config-problem.xml
new file mode 100644
index 0000000000..e6fb45c806
--- /dev/null
+++ b/src/test/java/org/mybatis/spring/mybatis-config-problem.xml
@@ -0,0 +1,37 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+