fix: throw exception when accessing result props incorrectly
This commit is contained in:
@@ -6,21 +6,54 @@ using AnthropicClient.Models;
|
||||
/// <typeparam name="T">The type of the result value.</typeparam>
|
||||
public class AnthropicResult<T>
|
||||
{
|
||||
private T _value = default!;
|
||||
|
||||
/// <summary>
|
||||
/// The value of the result.
|
||||
/// </summary>
|
||||
public T Value { get; }
|
||||
/// <exception cref="InvalidOperationException">Thrown when the result is not successful.</exception>
|
||||
public T Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return IsSuccess ? _value : throw new InvalidOperationException("The result is not successful. Check the error property for more information.");
|
||||
}
|
||||
|
||||
private set
|
||||
{
|
||||
_value = value;
|
||||
}
|
||||
}
|
||||
|
||||
private AnthropicError _error = default!;
|
||||
|
||||
/// <summary>
|
||||
/// The error of the result.
|
||||
/// </summary>
|
||||
public AnthropicError Error { get; }
|
||||
/// <exception cref="InvalidOperationException">Thrown when the result is successful.</exception>
|
||||
public AnthropicError Error
|
||||
{
|
||||
get
|
||||
{
|
||||
return IsSuccess ? throw new InvalidOperationException("The result is successful. Check the value property for more information.") : _error;
|
||||
}
|
||||
|
||||
private set
|
||||
{
|
||||
_error = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the operation was successful.
|
||||
/// </summary>
|
||||
public bool IsSuccess { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the operation failed.
|
||||
/// </summary>
|
||||
public bool IsFailure => !IsSuccess;
|
||||
|
||||
/// <summary>
|
||||
/// The request ID of the operation.
|
||||
/// </summary>
|
||||
|
||||
@@ -5,21 +5,54 @@ namespace AnthropicClient.Models;
|
||||
/// </summary>
|
||||
public class ToolCallResult<T>
|
||||
{
|
||||
private T? _value = default!;
|
||||
|
||||
/// <summary>
|
||||
/// The value of the tool call result. Can be null if the call failed, the call was successful but the return type is void or Task, or the call was successful but the return value is null
|
||||
/// </summary>
|
||||
public T? Value { get; }
|
||||
/// <exception cref="InvalidOperationException">Thrown when the result is not successful.</exception>
|
||||
public T? Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return IsSuccess ? _value : throw new InvalidOperationException("The result is not successful. Check the error property for more information.");
|
||||
}
|
||||
|
||||
private set
|
||||
{
|
||||
_value = value;
|
||||
}
|
||||
}
|
||||
|
||||
private Exception _error = default!;
|
||||
|
||||
/// <summary>
|
||||
/// The error of the tool call result.
|
||||
/// </summary>
|
||||
public Exception Error { get; }
|
||||
/// <exception cref="InvalidOperationException">Thrown when the result is successful.</exception>
|
||||
public Exception Error
|
||||
{
|
||||
get
|
||||
{
|
||||
return IsSuccess ? throw new InvalidOperationException("The result is successful. Check the value property for more information.") : _error;
|
||||
}
|
||||
|
||||
private set
|
||||
{
|
||||
_error = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the tool call was successful.
|
||||
/// </summary>
|
||||
public bool IsSuccess { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the tool call failed.
|
||||
/// </summary>
|
||||
public bool IsFailure => !IsSuccess;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ToolCallResult{T}"/> class.
|
||||
/// </summary>
|
||||
|
||||
@@ -154,7 +154,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
|
||||
"Get Weather",
|
||||
"Get the weather for a location in the specified units",
|
||||
typeof(GetWeatherTool),
|
||||
nameof(GetWeatherTool.GetWeather)
|
||||
nameof(GetWeatherTool.GetWeatherStatically)
|
||||
);
|
||||
|
||||
var response = await _client.CreateMessageAsync(new MessageRequest(
|
||||
|
||||
@@ -69,7 +69,6 @@ public class AnthropicApiClientTests : IntegrationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().BeOfType<MessageResponse>();
|
||||
result.Error.Should().BeNull();
|
||||
|
||||
var message = result.Value;
|
||||
message.Id.Should().Be("msg_013Zva2CMHLNnXjNJJKqJ2EF");
|
||||
@@ -135,7 +134,6 @@ public class AnthropicApiClientTests : IntegrationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().BeOfType<MessageResponse>();
|
||||
result.Error.Should().BeNull();
|
||||
|
||||
var message = result.Value;
|
||||
message.Id.Should().Be("msg_01D7FLrfh4GYq7yT1ULFeyMV");
|
||||
@@ -163,7 +161,6 @@ public class AnthropicApiClientTests : IntegrationTest
|
||||
var toolCallResult = await message.ToolCall!.InvokeAsync();
|
||||
toolCallResult.IsSuccess.Should().BeTrue();
|
||||
toolCallResult.Value!.ToString().Should().Be("^GSPC");
|
||||
toolCallResult.Error.Should().BeNull();
|
||||
}
|
||||
|
||||
|
||||
@@ -208,7 +205,6 @@ public class AnthropicApiClientTests : IntegrationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().BeOfType<MessageResponse>();
|
||||
result.Error.Should().BeNull();
|
||||
|
||||
var message = result.Value;
|
||||
message.Id.Should().Be("msg_01D7FLrfh4GYq7yT1ULFeyMV");
|
||||
@@ -323,7 +319,6 @@ public class AnthropicApiClientTests : IntegrationTest
|
||||
var toolCallResult = await toolCall!.InvokeAsync<string>();
|
||||
|
||||
toolCallResult.IsSuccess.Should().BeTrue();
|
||||
toolCallResult.Error.Should().BeNull();
|
||||
toolCallResult.Value.Should().Be(getWeather("San Francisco, CA","fahrenheit"));
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,12 @@ public class AnthropicResultTests
|
||||
var actual = AnthropicResult<string>.Success(value, headers);
|
||||
|
||||
actual.IsSuccess.Should().BeTrue();
|
||||
actual.IsFailure.Should().BeFalse();
|
||||
actual.Value.Should().Be(value);
|
||||
|
||||
var action = () => { var _ = actual.Error; };
|
||||
action.Should().Throw<InvalidOperationException>();
|
||||
|
||||
actual.Headers.Should().Be(headers);
|
||||
}
|
||||
|
||||
@@ -24,7 +29,12 @@ public class AnthropicResultTests
|
||||
var actual = AnthropicResult<string>.Failure(error, headers);
|
||||
|
||||
actual.IsSuccess.Should().BeFalse();
|
||||
actual.IsFailure.Should().BeTrue();
|
||||
actual.Error.Should().Be(error);
|
||||
|
||||
var action = () => { var _ = actual.Value; };
|
||||
action.Should().Throw<InvalidOperationException>();
|
||||
|
||||
actual.Headers.Should().Be(headers);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
namespace AnthropicClient.Tests.Unit.Models;
|
||||
|
||||
public class ToolCallResultTests
|
||||
{
|
||||
[Fact]
|
||||
public void Success_WhenCalled_ItShouldReturnSuccessResult()
|
||||
{
|
||||
var value = "success";
|
||||
|
||||
var actual = ToolCallResult<string>.Success(value);
|
||||
|
||||
actual.IsSuccess.Should().BeTrue();
|
||||
actual.IsFailure.Should().BeFalse();
|
||||
actual.Value.Should().Be(value);
|
||||
|
||||
var action = () => { var _ = actual.Error; };
|
||||
action.Should().Throw<InvalidOperationException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Failure_WhenCalled_ItShouldReturnFailureResult()
|
||||
{
|
||||
var error = new Exception();
|
||||
|
||||
var actual = ToolCallResult<string>.Failure(error);
|
||||
|
||||
actual.IsSuccess.Should().BeFalse();
|
||||
actual.IsFailure.Should().BeTrue();
|
||||
actual.Error.Should().Be(error);
|
||||
|
||||
var action = () => { var _ = actual.Value; };
|
||||
action.Should().Throw<InvalidOperationException>();
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,6 @@ public class ToolCallTests : SerializationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().Be("Hello, World!");
|
||||
result.Error.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -30,7 +29,6 @@ public class ToolCallTests : SerializationTest
|
||||
var result = await toolCall.InvokeAsync();
|
||||
|
||||
result.IsSuccess.Should().BeFalse();
|
||||
result.Value.Should().BeNull();
|
||||
result.Error.Should().NotBeNull();
|
||||
}
|
||||
|
||||
@@ -46,7 +44,6 @@ public class ToolCallTests : SerializationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().Be("Hello, World!");
|
||||
result.Error.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -60,7 +57,6 @@ public class ToolCallTests : SerializationTest
|
||||
var result = await toolCall.InvokeAsync();
|
||||
|
||||
result.IsSuccess.Should().BeFalse();
|
||||
result.Value.Should().BeNull();
|
||||
result.Error.Should().NotBeNull();
|
||||
}
|
||||
|
||||
@@ -78,7 +74,6 @@ public class ToolCallTests : SerializationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().Be("42");
|
||||
result.Error.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -94,7 +89,6 @@ public class ToolCallTests : SerializationTest
|
||||
var result = await toolCall.InvokeAsync();
|
||||
|
||||
result.IsSuccess.Should().BeFalse();
|
||||
result.Value.Should().BeNull();
|
||||
result.Error.Should().NotBeNull();
|
||||
}
|
||||
|
||||
@@ -112,7 +106,6 @@ public class ToolCallTests : SerializationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().Be("42");
|
||||
result.Error.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -128,7 +121,6 @@ public class ToolCallTests : SerializationTest
|
||||
var result = await toolCall.InvokeAsync();
|
||||
|
||||
result.IsSuccess.Should().BeFalse();
|
||||
result.Value.Should().BeNull();
|
||||
result.Error.Should().NotBeNull();
|
||||
}
|
||||
|
||||
@@ -146,7 +138,6 @@ public class ToolCallTests : SerializationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().Be("42");
|
||||
result.Error.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -162,7 +153,6 @@ public class ToolCallTests : SerializationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().Be("42");
|
||||
result.Error.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -177,7 +167,6 @@ public class ToolCallTests : SerializationTest
|
||||
var result = await toolCall.InvokeAsync();
|
||||
|
||||
result.IsSuccess.Should().BeFalse();
|
||||
result.Value.Should().BeNull();
|
||||
result.Error.Should().NotBeNull();
|
||||
}
|
||||
|
||||
@@ -195,7 +184,6 @@ public class ToolCallTests : SerializationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().Be("Monday");
|
||||
result.Error.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -219,7 +207,6 @@ public class ToolCallTests : SerializationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().Be("John");
|
||||
result.Error.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -236,7 +223,6 @@ public class ToolCallTests : SerializationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().BeNull();
|
||||
result.Error.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -253,7 +239,6 @@ public class ToolCallTests : SerializationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().BeNull();
|
||||
result.Error.Should().BeNull();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -301,7 +301,7 @@ public class ToolTests : SerializationTest
|
||||
tool.DisplayName.Should().Be("Name");
|
||||
tool.Description.Should().Be("Description");
|
||||
tool.Function.Method.Name.Should().Be(nameof(ProperTool.GetWeather));
|
||||
tool.Function.Instance.Should().BeNull();
|
||||
tool.Function.Instance.Should().BeOfType<ProperTool>();
|
||||
tool.InputSchema.Should().BeEquivalentTo(
|
||||
expectedSchema,
|
||||
t => t.IgnoringCyclicReferences()
|
||||
|
||||
Reference in New Issue
Block a user