Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/mybatis/spring/TestProblemMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2010-2016 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.spring.TestProblemMapper">

<select id="findProblemTest">
SELECT *
FROM table_test WHERE id = #{}
</select>
</mapper>
37 changes: 37 additions & 0 deletions src/test/java/org/mybatis/spring/mybatis-config-problem.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2010-2016 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<settings>
<!-- changes from the defaults for testing -->
<setting name="cacheEnabled" value="false" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
<setting name="vfsImpl" value="org.apache.ibatis.io.JBoss6VFS"/>
</settings>

<mappers>
<mapper resource="org/mybatis/spring/TestProblemMapper.xml" />
</mappers>

</configuration>