minimal improvements for ApiResponse

This commit is contained in:
Pascal Schedel
2024-10-23 12:51:36 +02:00
parent 3cec6983b8
commit 59e9499d92
3 changed files with 20 additions and 3 deletions

View File

@@ -0,0 +1,16 @@
namespace Pilz.Net.Api;
public class ApiException : Exception
{
public ApiException()
{
}
public ApiException(string? message) : base(message)
{
}
public ApiException(string? message, Exception? innerException) : base(message, innerException)
{
}
}

View File

@@ -10,6 +10,6 @@ public record class ApiResponse(
public void EnsureOk()
{
if (!IsOk)
throw new Exception("Api return is not ok!");
throw new ApiException("Api return is not ok: " + StatusCode);
}
}

View File

@@ -12,9 +12,10 @@ public record class ApiResponse<T>(
public bool IsOk => StatusCode == HttpStatusCode.OK && Message is not null;
[MemberNotNull(nameof(Message))]
public void EnsureOk()
public T EnsureOk()
{
if (!IsOk)
throw new Exception("Api return is not ok!");
throw new ApiException("Api return is not ok: " + StatusCode);
return Message;
}
}