Skip to content

Commit

Permalink
feat(api): add AlgoApiException that can be thrown from an `ErrorRe…
Browse files Browse the repository at this point in the history
…sponse`
  • Loading branch information
jasonboukheir committed Jun 19, 2022
1 parent 169be0b commit 2eb8209
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Runtime/CareBoo.AlgoSdk/NodeServices/Networking/ErrorResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@

namespace AlgoSdk
{
/// <summary>
/// An Exception thrown from an <see cref="ErrorResponse"/>.
/// </summary>
public class AlgoApiException : Exception
{
readonly ErrorResponse error;

public AlgoApiException(ErrorResponse error) : base(error.Message)
{
this.error = error;
}

/// <summary>
/// The <see cref="ErrorResponse"/> that threw this exception.
/// </summary>
public ErrorResponse Error => error;
}

/// <summary>
/// An error response from algorand APIs with optional data field.
/// </summary>
Expand Down Expand Up @@ -41,6 +59,14 @@ public override string ToString()
return Message;
}

public void ThrowIfError()
{
if (this)
{
throw new AlgoApiException(this);
}
}

public bool IsError => Code >= 400 || !string.IsNullOrWhiteSpace(Message);

public static implicit operator bool(ErrorResponse error)
Expand Down

0 comments on commit 2eb8209

Please sign in to comment.