/// <summary> /// Switch to <paramref name="ifError"/> flow if response returns error /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="errorPredicate">Predicate for error to execute <paramref name="ifError"/></param> /// <param name="ifError">Flow to execute in case of error</param> /// <returns></returns> public static ValueTask <ApiResponse <T> > IfError <T>( this ApiResponse <T> source, CodeErrorPredicate errorPredicate, Func <Error, ValueTask <ApiResponse <T> > > ifError) { if (source.Error is not null && errorPredicate.IsMatch(source.Error)) { return(ifError(source.Error)); } return(new ValueTask <ApiResponse <T> >(source)); }
/// <summary> /// Execute <paramref name="onError"/> if response has errors /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="errorPredicate">Error predicate</param> /// <param name="onError"></param> /// <returns></returns> public static async Task <ApiResponse <T> > OnError <T>( this ValueTask <ApiResponse <T> > source, CodeErrorPredicate errorPredicate, Func <Error, Task> onError ) { var response = await source.ConfigureAwait(false); return(await response .OnError(errorPredicate, onError) .ConfigureAwait(false)); }
/// <summary> /// Switch to <paramref name="ifError"/> flow if response returns error /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="errorPredicate">Predicate for error to execute <paramref name="ifError"/></param> /// <param name="ifError">Flow to execute in case of error</param> /// <returns></returns> public static async Task <ApiResponse <T> > IfError <T>( this ApiResponse <T> source, CodeErrorPredicate errorPredicate, Func <Error, Task <ApiResponse <T> > > ifError) { if (source.Error is not null && errorPredicate.IsMatch(source.Error)) { return(await ifError(source.Error).ConfigureAwait(false)); } return(source); }
/// <summary> /// Return ok response and suppress errors that match <paramref name="errorPredicate"/> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="errorPredicate">Predicate for errors</param> /// <returns></returns> public static async ValueTask <ApiResponse <Unit> > SuppressErrors <T>( this ValueTask <ApiResponse <T> > source, CodeErrorPredicate errorPredicate) { var response = await source.ConfigureAwait(false); if (response.HasErrors && errorPredicate.IsMatch(response.Error)) { return(ApiResponse.Ok()); } return(response.AsUnit()); }
/// <summary> /// Execute <paramref name="onError"/> if response has errors /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="errorPredicate">Error predicate</param> /// <param name="onError"></param> /// <returns></returns> public static async ValueTask <ApiResponse <T> > OnError <T>( this ApiResponse <T> source, CodeErrorPredicate errorPredicate, Func <Error, ValueTask> onError ) { if (source.Error is not null && errorPredicate.IsMatch(source.Error)) { await onError(source.Error).ConfigureAwait(false); } return(source); }
/// <summary> /// Execute <paramref name="onError"/> if response has errors /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="errorPredicate">Error predicate</param> /// <param name="onError"></param> /// <returns></returns> public static ApiResponse <T> OnError <T>( this ApiResponse <T> source, CodeErrorPredicate errorPredicate, Action <Error> onError ) { if (source.Error is not null && errorPredicate.IsMatch(source.Error)) { onError(source.Error); } return(source); }