Skip to content

Commit 84cfb2c

Browse files
committed
Implement UnwrapOrThrow
1 parent 627335c commit 84cfb2c

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/Initium/Results/ServiceResult.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,20 @@ public class ServiceResult<TData> : ServiceResult
209209
Success = true,
210210
Data = data
211211
};
212-
212+
213+
/// <summary>
214+
/// Tries to unwrap the data contained in the result, returning null if the data is null.
215+
/// </summary>
216+
/// <returns>The unwrapped data or null.</returns>
217+
public TData? Unwrap() => Data;
218+
213219
/// <summary>
214-
/// Unwraps the data contained in the result, throwing an exception if the result is unsuccessful or the data is null.
220+
/// Unwraps the data contained in the result, throwing an exception if the data is null.
221+
/// Allows overriding the HTTP status code and message.
215222
/// </summary>
223+
/// <param name="statusCode">The HTTP status code for the exception. Defaults to InternalServerError.</param>
224+
/// <param name="message">The custom message for the exception. Defaults to a generic message.</param>
216225
/// <returns>The unwrapped data.</returns>
217-
/// <exception cref="ApiException">Thrown if the result is unsuccessful or the data is null.</exception>
218-
public TData Unwrap() => Data ?? throw new ApiException(HttpStatusCode.InternalServerError, "Unwrapping failed, data is null.");
226+
/// <exception cref="ApiException">Thrown if the data is null.</exception>
227+
public TData UnwrapOrThrow(HttpStatusCode statusCode = HttpStatusCode.InternalServerError, string? message = null) => Data ?? throw new ApiException(statusCode, message ?? "Unwrapping failed, data is null.");
219228
}

0 commit comments

Comments
 (0)