forked from Mirror/ollama4j
Handle all exceptions when loading image URLs in chat builder
Replaced separate IOException and InterruptedException handling with a single catch for Exception in OllamaChatRequestBuilder, ensuring that any failure when loading image URLs is logged and does not break message building. Updated related unit test to verify builder robustness and usability after invalid image URL input.
This commit is contained in:
@@ -40,28 +40,21 @@ class TestOllamaChatRequestBuilder {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testImageUrlFailuresAreIgnoredAndDoNotBreakBuild() {
|
||||
// Provide a syntactically invalid URL, but catch the expected exception to verify builder
|
||||
// robustness
|
||||
void testImageUrlFailuresAreHandledAndBuilderRemainsUsable() {
|
||||
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance("m");
|
||||
try {
|
||||
builder.withMessage(
|
||||
OllamaChatMessageRole.USER,
|
||||
"hi",
|
||||
Collections.emptyList(),
|
||||
"ht!tp://invalid url \n not a uri");
|
||||
fail("Expected IllegalArgumentException due to malformed URL");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// Expected: malformed URL should throw IllegalArgumentException
|
||||
}
|
||||
String invalidUrl = "ht!tp:/bad_url"; // clearly invalid URL format
|
||||
|
||||
// No exception should be thrown; builder should handle invalid URL gracefully
|
||||
builder.withMessage(OllamaChatMessageRole.USER, "hi", Collections.emptyList(), invalidUrl);
|
||||
|
||||
// The builder should still be usable after the exception
|
||||
OllamaChatRequest req =
|
||||
builder.withMessage(OllamaChatMessageRole.USER, "hello", Collections.emptyList())
|
||||
.build();
|
||||
|
||||
assertNotNull(req.getMessages());
|
||||
assertEquals(1, req.getMessages().size());
|
||||
assert (!req.getMessages().isEmpty());
|
||||
OllamaChatMessage msg = req.getMessages().get(0);
|
||||
assertEquals("hello", msg.getContent());
|
||||
assertNotNull(msg.getContent());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user