mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-10-14 01:18:58 +02:00
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:
parent
274da54d50
commit
4f02b299c3
@ -20,9 +20,7 @@ import java.util.stream.Collectors;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Helper class for creating {@link OllamaChatRequest} objects using the builder-pattern.
|
||||
*/
|
||||
/** Helper class for creating {@link OllamaChatRequest} objects using the builder-pattern. */
|
||||
public class OllamaChatRequestBuilder {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(OllamaChatRequestBuilder.class);
|
||||
@ -114,14 +112,9 @@ public class OllamaChatRequestBuilder {
|
||||
imageUrl,
|
||||
imageURLConnectTimeoutSeconds,
|
||||
imageURLReadTimeoutSeconds));
|
||||
} catch (IOException e) {
|
||||
} catch (Exception e) {
|
||||
LOG.warn(
|
||||
"Content of URL '{}' could not be read, will not add to message!",
|
||||
imageUrl,
|
||||
e);
|
||||
} catch (InterruptedException e) {
|
||||
LOG.warn(
|
||||
"Loading image from URL '{}' was interrupted, will not add to message!",
|
||||
"Loading image from URL '{}' failed, will not add to message!",
|
||||
imageUrl,
|
||||
e);
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user