Skip to content

Commit 86ba591

Browse files
committed
Enhance ServiceResult
1 parent 1ba0ee0 commit 86ba591

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/Initium/Results/ServiceResult.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,45 @@ public class ServiceResult : BaseResult
6767
StatusCode = statusCode
6868
};
6969

70+
71+
/// <summary>
72+
/// Creates a failed <see cref="ServiceResult"/> with an exception message.
73+
/// </summary>
74+
/// <param name="exception">The exception whose message will be used to describe the error.</param>
75+
/// <returns>A failed <see cref="ServiceResult"/> with an exception message.</returns>
76+
public static ServiceResult Error(Exception exception) => new()
77+
{
78+
Success = false,
79+
Message = exception.Message,
80+
StatusCode = HttpStatusCode.InternalServerError
81+
};
82+
7083
/// <summary>
7184
/// Chains the current result with another operation if the current result is successful.
7285
/// </summary>
7386
/// <param name="next">A function returning the next <see cref="ServiceResult"/>.</param>
7487
/// <returns>The result of the next operation if the current result is successful; otherwise, the current result.</returns>
7588
public ServiceResult ChainWith(Func<ServiceResult> next) => !this ? this : next();
7689

90+
public ServiceResult WithMessage(string message)
91+
{
92+
Message = message;
93+
return this;
94+
}
95+
96+
public ServiceResult WithStatusCode(HttpStatusCode statusCode)
97+
{
98+
StatusCode = statusCode;
99+
return this;
100+
}
101+
102+
public ServiceResult WithMetadata(string key, string value)
103+
{
104+
Metadata ??= new Dictionary<string, string>();
105+
Metadata[key] = value;
106+
return this;
107+
}
108+
77109
/// <summary>
78110
/// Converts the current <see cref="ServiceResult"/> into an <see cref="ActionResult"/> for use in ASP.NET Core.
79111
/// </summary>

0 commit comments

Comments
 (0)