Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 46fec41

Browse files
committed
#122 Now catching exceptions from loading non-REST modules
1 parent c0f15ef commit 46fec41

File tree

4 files changed

+55
-20
lines changed

4 files changed

+55
-20
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
group=com.marklogic
22
javadocsDir=../gh-pages-marklogic-java/javadocs
3-
version=3.13.3
3+
version=3.13.4-SNAPSHOT

src/main/java/com/marklogic/client/ext/modulesloader/impl/DefaultModulesLoader.java

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -358,26 +358,21 @@ protected void loadAssets(Modules modules, Set<Resource> loadedModules) {
358358
}
359359

360360
if (includeFilenamePattern != null) {
361-
// Make sure the DocumentFileReader is not null
362-
assetFileLoader.initializeDocumentFileReader();
363-
DocumentFileReader dfr = assetFileLoader.getDocumentFileReader();
364-
if (dfr instanceof DefaultDocumentFileReader) {
365-
DefaultDocumentFileReader reader = (DefaultDocumentFileReader) dfr;
366-
reader.addDocumentFileProcessor(documentFile -> {
367-
File f = documentFile.getFile();
368-
if (f == null) {
369-
return null;
370-
}
371-
if (!includeFilenamePattern.matcher(f.getAbsolutePath()).matches()) {
372-
return null;
373-
}
374-
return documentFile;
375-
});
361+
applyIncludeFilenamePattern();
362+
}
363+
364+
List<DocumentFile> list = null;
365+
try {
366+
list = assetFileLoader.loadFiles(paths);
367+
} catch (RuntimeException ex) {
368+
if (catchExceptions) {
369+
logger.error("Error loading modules: " + ex.getMessage());
370+
} else {
371+
throw ex;
376372
}
377373
}
378374

379-
List<DocumentFile> list = assetFileLoader.loadFiles(paths);
380-
if (staticChecker != null && !list.isEmpty()) {
375+
if (staticChecker != null && list != null && !list.isEmpty()) {
381376
try {
382377
staticChecker.checkLoadedAssets(list);
383378
} catch (RuntimeException ex) {
@@ -389,8 +384,29 @@ protected void loadAssets(Modules modules, Set<Resource> loadedModules) {
389384
}
390385
}
391386

392-
for (DocumentFile asset : list) {
393-
loadedModules.add(asset.getResource());
387+
if (list != null) {
388+
for (DocumentFile asset : list) {
389+
loadedModules.add(asset.getResource());
390+
}
391+
}
392+
}
393+
394+
protected void applyIncludeFilenamePattern() {
395+
// Make sure the DocumentFileReader is not null
396+
assetFileLoader.initializeDocumentFileReader();
397+
DocumentFileReader dfr = assetFileLoader.getDocumentFileReader();
398+
if (dfr instanceof DefaultDocumentFileReader) {
399+
DefaultDocumentFileReader reader = (DefaultDocumentFileReader) dfr;
400+
reader.addDocumentFileProcessor(documentFile -> {
401+
File f = documentFile.getFile();
402+
if (f == null) {
403+
return null;
404+
}
405+
if (!includeFilenamePattern.matcher(f.getAbsolutePath()).matches()) {
406+
return null;
407+
}
408+
return documentFile;
409+
});
394410
}
395411
}
396412

src/test/java/com/marklogic/client/ext/modulesloader/impl/LoadModulesTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ public void invalidRestModule() {
139139
modulesLoader.loadModules(dir, new DefaultModulesFinder(), client);
140140
}
141141

142+
@Test
143+
public void catchExceptionsForInvalidModules() {
144+
String dir = Paths.get("src", "test", "resources", "bad-modules").toString();
145+
146+
try {
147+
modulesLoader.loadModules(dir, new DefaultModulesFinder(), client);
148+
fail("Loading modules should have failed because of an invalid JSON file");
149+
} catch (RuntimeException re) {
150+
assertTrue(re.getMessage().contains("Unexpected end of file in JSON"));
151+
}
152+
153+
modulesLoader.setCatchExceptions(true);
154+
// This should now succeed since we're catching exceptions
155+
Set<Resource> loadedModules = modulesLoader.loadModules(dir, new DefaultModulesFinder(), client);
156+
assertTrue("There's only the one invalid module, so nothing should have been loaded", loadedModules.isEmpty());
157+
}
158+
142159
/**
143160
* This test is a little brittle because it assumes the URI of options/services/transforms that are loaded
144161
* into the Modules database.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
"this is invalid":

0 commit comments

Comments
 (0)