@@ -8,7 +8,10 @@ public static partial class ResultExtensions
88 /// <summary>
99 /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
1010 /// </summary>
11- public static async Task < Result > MapError ( this Task < Result > resultTask , Func < string , string > errorFactory )
11+ public static async Task < Result > MapError (
12+ this Task < Result > resultTask ,
13+ Func < string , string > errorFactory
14+ )
1215 {
1316 var result = await resultTask . DefaultAwait ( ) ;
1417 if ( result . IsSuccess )
@@ -20,10 +23,29 @@ public static async Task<Result> MapError(this Task<Result> resultTask, Func<str
2023 return Result . Failure ( error ) ;
2124 }
2225
26+ public static async Task < Result > MapError < TContext > (
27+ this Task < Result > resultTask ,
28+ Func < string , TContext , string > errorFactory ,
29+ TContext context
30+ )
31+ {
32+ var result = await resultTask . DefaultAwait ( ) ;
33+ if ( result . IsSuccess )
34+ {
35+ return Result . Success ( ) ;
36+ }
37+
38+ var error = errorFactory ( result . Error , context ) ;
39+ return Result . Failure ( error ) ;
40+ }
41+
2342 /// <summary>
2443 /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
2544 /// </summary>
26- public static async Task < UnitResult < E > > MapError < E > ( this Task < Result > resultTask , Func < string , E > errorFactory )
45+ public static async Task < UnitResult < E > > MapError < E > (
46+ this Task < Result > resultTask ,
47+ Func < string , E > errorFactory
48+ )
2749 {
2850 var result = await resultTask . DefaultAwait ( ) ;
2951 if ( result . IsSuccess )
@@ -35,10 +57,29 @@ public static async Task<UnitResult<E>> MapError<E>(this Task<Result> resultTask
3557 return UnitResult . Failure ( error ) ;
3658 }
3759
60+ public static async Task < UnitResult < E > > MapError < E , TContext > (
61+ this Task < Result > resultTask ,
62+ Func < string , TContext , E > errorFactory ,
63+ TContext context
64+ )
65+ {
66+ var result = await resultTask . DefaultAwait ( ) ;
67+ if ( result . IsSuccess )
68+ {
69+ return UnitResult . Success < E > ( ) ;
70+ }
71+
72+ var error = errorFactory ( result . Error , context ) ;
73+ return UnitResult . Failure ( error ) ;
74+ }
75+
3876 /// <summary>
3977 /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
4078 /// </summary>
41- public static async Task < Result < T > > MapError < T > ( this Task < Result < T > > resultTask , Func < string , string > errorFactory )
79+ public static async Task < Result < T > > MapError < T > (
80+ this Task < Result < T > > resultTask ,
81+ Func < string , string > errorFactory
82+ )
4283 {
4384 var result = await resultTask . DefaultAwait ( ) ;
4485 if ( result . IsSuccess )
@@ -50,10 +91,29 @@ public static async Task<Result<T>> MapError<T>(this Task<Result<T>> resultTask,
5091 return Result . Failure < T > ( error ) ;
5192 }
5293
94+ public static async Task < Result < T > > MapError < T , TContext > (
95+ this Task < Result < T > > resultTask ,
96+ Func < string , TContext , string > errorFactory ,
97+ TContext context
98+ )
99+ {
100+ var result = await resultTask . DefaultAwait ( ) ;
101+ if ( result . IsSuccess )
102+ {
103+ return Result . Success ( result . Value ) ;
104+ }
105+
106+ var error = errorFactory ( result . Error , context ) ;
107+ return Result . Failure < T > ( error ) ;
108+ }
109+
53110 /// <summary>
54111 /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
55112 /// </summary>
56- public static async Task < Result < T , E > > MapError < T , E > ( this Task < Result < T > > resultTask , Func < string , E > errorFactory )
113+ public static async Task < Result < T , E > > MapError < T , E > (
114+ this Task < Result < T > > resultTask ,
115+ Func < string , E > errorFactory
116+ )
57117 {
58118 var result = await resultTask . DefaultAwait ( ) ;
59119 if ( result . IsSuccess )
@@ -65,10 +125,29 @@ public static async Task<Result<T, E>> MapError<T, E>(this Task<Result<T>> resul
65125 return Result . Failure < T , E > ( error ) ;
66126 }
67127
128+ public static async Task < Result < T , E > > MapError < T , E , TContext > (
129+ this Task < Result < T > > resultTask ,
130+ Func < string , TContext , E > errorFactory ,
131+ TContext context
132+ )
133+ {
134+ var result = await resultTask . DefaultAwait ( ) ;
135+ if ( result . IsSuccess )
136+ {
137+ return Result . Success < T , E > ( result . Value ) ;
138+ }
139+
140+ var error = errorFactory ( result . Error , context ) ;
141+ return Result . Failure < T , E > ( error ) ;
142+ }
143+
68144 /// <summary>
69145 /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
70146 /// </summary>
71- public static async Task < Result > MapError < E > ( this Task < UnitResult < E > > resultTask , Func < E , string > errorFactory )
147+ public static async Task < Result > MapError < E > (
148+ this Task < UnitResult < E > > resultTask ,
149+ Func < E , string > errorFactory
150+ )
72151 {
73152 var result = await resultTask . DefaultAwait ( ) ;
74153 if ( result . IsSuccess )
@@ -80,10 +159,29 @@ public static async Task<Result> MapError<E>(this Task<UnitResult<E>> resultTask
80159 return Result . Failure ( error ) ;
81160 }
82161
162+ public static async Task < Result > MapError < E , TContext > (
163+ this Task < UnitResult < E > > resultTask ,
164+ Func < E , TContext , string > errorFactory ,
165+ TContext context
166+ )
167+ {
168+ var result = await resultTask . DefaultAwait ( ) ;
169+ if ( result . IsSuccess )
170+ {
171+ return Result . Success ( ) ;
172+ }
173+
174+ var error = errorFactory ( result . Error , context ) ;
175+ return Result . Failure ( error ) ;
176+ }
177+
83178 /// <summary>
84179 /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
85180 /// </summary>
86- public static async Task < UnitResult < E2 > > MapError < E , E2 > ( this Task < UnitResult < E > > resultTask , Func < E , E2 > errorFactory )
181+ public static async Task < UnitResult < E2 > > MapError < E , E2 > (
182+ this Task < UnitResult < E > > resultTask ,
183+ Func < E , E2 > errorFactory
184+ )
87185 {
88186 var result = await resultTask . DefaultAwait ( ) ;
89187 if ( result . IsSuccess )
@@ -95,10 +193,29 @@ public static async Task<UnitResult<E2>> MapError<E, E2>(this Task<UnitResult<E>
95193 return UnitResult . Failure ( error ) ;
96194 }
97195
196+ public static async Task < UnitResult < E2 > > MapError < E , E2 , TContext > (
197+ this Task < UnitResult < E > > resultTask ,
198+ Func < E , TContext , E2 > errorFactory ,
199+ TContext context
200+ )
201+ {
202+ var result = await resultTask . DefaultAwait ( ) ;
203+ if ( result . IsSuccess )
204+ {
205+ return UnitResult . Success < E2 > ( ) ;
206+ }
207+
208+ var error = errorFactory ( result . Error , context ) ;
209+ return UnitResult . Failure ( error ) ;
210+ }
211+
98212 /// <summary>
99213 /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
100214 /// </summary>
101- public static async Task < Result < T > > MapError < T , E > ( this Task < Result < T , E > > resultTask , Func < E , string > errorFactory )
215+ public static async Task < Result < T > > MapError < T , E > (
216+ this Task < Result < T , E > > resultTask ,
217+ Func < E , string > errorFactory
218+ )
102219 {
103220 var result = await resultTask . DefaultAwait ( ) ;
104221 if ( result . IsSuccess )
@@ -110,10 +227,29 @@ public static async Task<Result<T>> MapError<T, E>(this Task<Result<T, E>> resul
110227 return Result . Failure < T > ( error ) ;
111228 }
112229
230+ public static async Task < Result < T > > MapError < T , E , TContext > (
231+ this Task < Result < T , E > > resultTask ,
232+ Func < E , TContext , string > errorFactory ,
233+ TContext context
234+ )
235+ {
236+ var result = await resultTask . DefaultAwait ( ) ;
237+ if ( result . IsSuccess )
238+ {
239+ return Result . Success ( result . Value ) ;
240+ }
241+
242+ var error = errorFactory ( result . Error , context ) ;
243+ return Result . Failure < T > ( error ) ;
244+ }
245+
113246 /// <summary>
114247 /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
115248 /// </summary>
116- public static async Task < Result < T , E2 > > MapError < T , E , E2 > ( this Task < Result < T , E > > resultTask , Func < E , E2 > errorFactory )
249+ public static async Task < Result < T , E2 > > MapError < T , E , E2 > (
250+ this Task < Result < T , E > > resultTask ,
251+ Func < E , E2 > errorFactory
252+ )
117253 {
118254 var result = await resultTask . DefaultAwait ( ) ;
119255 if ( result . IsSuccess )
@@ -124,5 +260,21 @@ public static async Task<Result<T, E2>> MapError<T, E, E2>(this Task<Result<T, E
124260 var error = errorFactory ( result . Error ) ;
125261 return Result . Failure < T , E2 > ( error ) ;
126262 }
263+
264+ public static async Task < Result < T , E2 > > MapError < T , E , E2 , TContext > (
265+ this Task < Result < T , E > > resultTask ,
266+ Func < E , TContext , E2 > errorFactory ,
267+ TContext context
268+ )
269+ {
270+ var result = await resultTask . DefaultAwait ( ) ;
271+ if ( result . IsSuccess )
272+ {
273+ return Result . Success < T , E2 > ( result . Value ) ;
274+ }
275+
276+ var error = errorFactory ( result . Error , context ) ;
277+ return Result . Failure < T , E2 > ( error ) ;
278+ }
127279 }
128- }
280+ }
0 commit comments