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>