/// <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, IErrorPredicate errorPredicate) { if (errorPredicate == null) { throw new ArgumentNullException(nameof(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, IErrorPredicate errorPredicate, Func <Error, ApiResponse <T> > ifError) { if (errorPredicate == null) { throw new ArgumentNullException(nameof(errorPredicate)); } 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, IErrorPredicate errorPredicate, Func <Error, Task <ApiResponse <T> > > ifError) { if (errorPredicate == null) { throw new ArgumentNullException(nameof(errorPredicate)); } if (source.Error is not null && errorPredicate.IsMatch(source.Error)) { return(await ifError(source.Error).ConfigureAwait(false)); } return(source); }