File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff 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>
You can’t perform that action at this time.
0 commit comments