fix: throw exception when accessing result props incorrectly

This commit is contained in:
Stevan Freeborn
2024-07-06 11:55:47 -05:00
parent 1d352a6f95
commit c538a3eb8c
8 changed files with 116 additions and 26 deletions
+35 -2
View File
@@ -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>
+35 -2
View File
@@ -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>