/// <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 ApiResponse <Unit> SuppressErrors <T>( this ApiResponse <T> source, TypeErrorPredicate errorPredicate) { if (source.Error is not null && errorPredicate.IsMatch(source.Error)) { return(ApiResponse.Ok()); } return(source.AsUnit()); }
/// <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 ApiResponse <T> IfError <T>( this ApiResponse <T> source, TypeErrorPredicate errorPredicate, Func <Error, ApiResponse <T> > ifError) { if (source.Error is not null && errorPredicate.IsMatch(source.Error)) { return(ifError(source.Error)); } return(source); }
/// <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, TypeErrorPredicate 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, TypeErrorPredicate errorPredicate) { var response = await source.ConfigureAwait(false); if (response.Error is not null && 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, TypeErrorPredicate 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, TypeErrorPredicate errorPredicate, Action <Error> onError ) { if (source.Error is not null && errorPredicate.IsMatch(source.Error)) { onError(source.Error); } return(source); }