Improve error handling and code clarity across modules

Enhanced error handling for image URL loading in OllamaChatRequestBuilder, ensuring exceptions are thrown and logged appropriately. Updated test cases to reflect new exception behavior. Improved documentation and code clarity in WeatherTool and test classes. Refactored JSON parsing in response models for conciseness. Minor cleanup in pom.xml and integration test comments for better maintainability.
This commit is contained in:
amithkoujalgi
2025-09-18 01:22:12 +05:30
parent fe87c4ccc8
commit 7788f954d6
15 changed files with 81 additions and 59 deletions

View File

@@ -619,25 +619,27 @@ class OllamaAPIIntegrationTest {
OllamaChatMessageRole.ASSISTANT.getRoleName(),
chatResult.getResponseModel().getMessage().getRole().getRoleName());
// Reproducing this scenario consistently is challenging, as the model's behavior can vary.
// Therefore, these checks are currently skipped until a more reliable approach is found.
// List<OllamaChatToolCalls> toolCalls =
// chatResult.getChatHistory().get(1).getToolCalls();
// assertEquals(1, toolCalls.size());
// OllamaToolCallsFunction function = toolCalls.get(0).getFunction();
// assertEquals("sayHello", function.getName());
// assertEquals(2, function.getArguments().size());
// Object name = function.getArguments().get("name");
// assertNotNull(name);
// assertEquals("Rahul", name);
// Object numberOfHearts = function.getArguments().get("numberOfHearts");
// assertNotNull(numberOfHearts);
// assertTrue(Integer.parseInt(numberOfHearts.toString()) > 1);
// assertTrue(chatResult.getChatHistory().size() > 2);
// List<OllamaChatToolCalls> finalToolCalls =
// chatResult.getResponseModel().getMessage().getToolCalls();
// assertNull(finalToolCalls);
/*
* Reproducing this scenario consistently is challenging, as the model's behavior can vary.
* Therefore, these checks are currently skipped until a more reliable approach is found.
*
* // List<OllamaChatToolCalls> toolCalls =
* // chatResult.getChatHistory().get(1).getToolCalls();
* // assertEquals(1, toolCalls.size());
* // OllamaToolCallsFunction function = toolCalls.get(0).getFunction();
* // assertEquals("sayHello", function.getName());
* // assertEquals(2, function.getArguments().size());
* // Object name = function.getArguments().get("name");
* // assertNotNull(name);
* // assertEquals("Rahul", name);
* // Object numberOfHearts = function.getArguments().get("numberOfHearts");
* // assertNotNull(numberOfHearts);
* // assertTrue(Integer.parseInt(numberOfHearts.toString()) > 1);
* // assertTrue(chatResult.getChatHistory().size() > 2);
* // List<OllamaChatToolCalls> finalToolCalls =
* // chatResult.getResponseModel().getMessage().getToolCalls();
* // assertNull(finalToolCalls);
*/
}
@Test

View File

@@ -40,14 +40,18 @@ class TestOllamaChatRequestBuilder {
}
@Test
void testImageUrlFailuresAreHandledAndBuilderRemainsUsable() {
void testImageUrlFailuresThrowExceptionAndBuilderRemainsUsable() {
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance("m");
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);
// Exception should be thrown for invalid URL
assertThrows(
Exception.class,
() -> {
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();

View File

@@ -50,9 +50,17 @@ class TestOllamaRequestBody {
}
@Override
public void onError(Throwable throwable) {}
// This method is intentionally left empty because, for this test,
// we do not expect any errors to occur during synchronous publishing.
// If an error does occur, the test will fail elsewhere.
public void onError(Throwable throwable) {
// No action needed for this test
}
@Override
// This method is intentionally left empty because for this test,
// all the data is synchronously delivered by the publisher, so no action is
// needed on completion.
public void onComplete() {}
});

View File

@@ -67,9 +67,9 @@ class TestOptionsAndUtils {
@Test
void testOptionsBuilderRejectsUnsupportedCustomType() {
OptionsBuilder builder = new OptionsBuilder();
assertThrows(
IllegalArgumentException.class, () -> builder.setCustomOption("bad", new Object()));
IllegalArgumentException.class,
() -> new OptionsBuilder().setCustomOption("bad", new Object()));
}
@Test

View File

@@ -104,11 +104,9 @@ public class TestChatRequestSerialization extends AbstractSerializationTest<Olla
assertThrowsExactly(
IllegalArgumentException.class,
() -> {
OllamaChatRequest req =
builder.withMessage(OllamaChatMessageRole.USER, "Some prompt")
.withOptions(
b.setCustomOption("cust_obj", new Object()).build())
.build();
builder.withMessage(OllamaChatMessageRole.USER, "Some prompt")
.withOptions(b.setCustomOption("cust_obj", new Object()).build())
.build();
});
}
@@ -120,7 +118,8 @@ public class TestChatRequestSerialization extends AbstractSerializationTest<Olla
.build();
String jsonRequest = serialize(req);
// no jackson deserialization as format property is not boolean ==> omit as deserialization
// no jackson deserialization as format property is not boolean ==> omit as
// deserialization
// of request is never used in real code anyways
JSONObject jsonObject = new JSONObject(jsonRequest);
String requestFormatProperty = jsonObject.getString("format");

View File

@@ -16,8 +16,7 @@ import io.github.ollama4j.utils.OptionsBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TestEmbedRequestSerialization
extends AbstractSerializationTest<OllamaEmbedRequestModel> {
class TestEmbedRequestSerialization extends AbstractSerializationTest<OllamaEmbedRequestModel> {
private OllamaEmbedRequestBuilder builder;

View File

@@ -17,8 +17,7 @@ import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TestGenerateRequestSerialization
extends AbstractSerializationTest<OllamaGenerateRequest> {
class TestGenerateRequestSerialization extends AbstractSerializationTest<OllamaGenerateRequest> {
private OllamaGenerateRequestBuilder builder;

View File

@@ -19,8 +19,7 @@ import org.junit.jupiter.api.Test;
* error responses from Ollama server that return HTTP 200 with error messages
* in the JSON body.
*/
public class TestModelPullResponseSerialization
extends AbstractSerializationTest<ModelPullResponse> {
class TestModelPullResponseSerialization extends AbstractSerializationTest<ModelPullResponse> {
/**
* Test the specific error case reported in GitHub issue #138.