| | | 1 | | namespace ZhEaIsNsAaBn.Utilities; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// The result of a fallible operation with no value: either success, or failure with one or more <see cref="Error"/>s |
| | | 5 | | /// (STYLE_GUIDE §3). Return an <see cref="Error"/> or a list of them directly; implicit conversions build the response. |
| | | 6 | | /// </summary> |
| | | 7 | | public class LayerResponse |
| | | 8 | | { |
| | | 9 | | /// <summary>Creates a response from one error.</summary> |
| | | 10 | | /// <param name="isSuccess">Whether the operation succeeded.</param> |
| | | 11 | | /// <param name="error"><see cref="Error.None"/> on success; a real error on failure.</param> |
| | | 12 | | /// <exception cref="InvalidOperationException">Success with a real error, or failure with <see cref="Error.None"/>. |
| | 36329 | 13 | | protected LayerResponse( bool isSuccess, Error error ) |
| | | 14 | | { |
| | 36329 | 15 | | if ( isSuccess && error != Error.None ) { throw new InvalidOperationException( "A successful response can't carr |
| | | 16 | | |
| | | 17 | | |
| | 36333 | 18 | | if ( !isSuccess && error == Error.None ) { throw new InvalidOperationException( "A failed response needs an erro |
| | | 19 | | |
| | | 20 | | |
| | | 21 | | IsSuccess = isSuccess; |
| | 36325 | 22 | | Errors = isSuccess ? [] : [ error ]; |
| | 36325 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary>Creates a response from a list of errors.</summary> |
| | | 26 | | /// <param name="isSuccess">Whether the operation succeeded.</param> |
| | | 27 | | /// <param name="errors">Empty or <see langword="null"/> on success; at least one real error on failure.</param> |
| | | 28 | | /// <exception cref="InvalidOperationException">Success with errors, or failure with no real error.</exception> |
| | 242 | 29 | | protected LayerResponse( bool isSuccess, List<Error>? errors = null ) |
| | | 30 | | { |
| | 242 | 31 | | if ( isSuccess && errors?.Count > 0 ) { throw new InvalidOperationException( "A successful response can't carry |
| | | 32 | | |
| | | 33 | | |
| | 242 | 34 | | if ( !isSuccess && ( errors is null || errors.Count == 0 || errors.Exists( e => e == Error.None ) ) ) |
| | | 35 | | { |
| | 4 | 36 | | throw new InvalidOperationException( "A failed response needs at least one real error." ); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | |
| | | 40 | | IsSuccess = isSuccess; |
| | 238 | 41 | | Errors = errors ?? []; |
| | 238 | 42 | | } |
| | | 43 | | |
| | | 44 | | |
| | | 45 | | /// <summary>The errors. Empty on success.</summary> |
| | | 46 | | public List<Error> Errors { get; } |
| | | 47 | | |
| | | 48 | | /// <summary>Whether the operation succeeded.</summary> |
| | | 49 | | public bool IsSuccess { get; } |
| | | 50 | | |
| | | 51 | | /// <summary>Whether the operation failed.</summary> |
| | 34709 | 52 | | public bool IsFailure => !IsSuccess; |
| | | 53 | | |
| | | 54 | | |
| | | 55 | | /// <summary>Creates a failed response.</summary> |
| | | 56 | | /// <param name="error">The error.</param> |
| | | 57 | | /// <returns>A failed response.</returns> |
| | 139 | 58 | | public static LayerResponse Failure( Error error ) => new( false, error ); |
| | | 59 | | |
| | | 60 | | /// <summary>Creates a failed response.</summary> |
| | | 61 | | /// <param name="errors">The errors; at least one.</param> |
| | | 62 | | /// <returns>A failed response.</returns> |
| | 40 | 63 | | public static LayerResponse Failure( List<Error> errors ) => new( false, errors ); |
| | | 64 | | |
| | | 65 | | /// <summary>Creates a successful response.</summary> |
| | | 66 | | /// <returns>A successful response.</returns> |
| | 902 | 67 | | public static LayerResponse Success() => new( true, Error.None ); |
| | | 68 | | |
| | | 69 | | /// <summary>Copies the outcome into a <see cref="LayerResponse{TResult}"/> with no value.</summary> |
| | | 70 | | /// <typeparam name="TResult">The value type of the new response.</typeparam> |
| | | 71 | | /// <param name="result">The response to copy.</param> |
| | | 72 | | /// <returns>Success, or failure with the same errors.</returns> |
| | | 73 | | public static LayerResponse<TResult> ToLayerResponse< TResult >( LayerResponse result ) |
| | 0 | 74 | | => result.IsSuccess ? LayerResponse<TResult>.Success() : LayerResponse<TResult>.Failure( result.Errors ); |
| | | 75 | | |
| | | 76 | | |
| | | 77 | | /// <summary>Wraps an error as a failed response.</summary> |
| | | 78 | | /// <param name="error">The error.</param> |
| | 24 | 79 | | public static implicit operator LayerResponse( Error error ) => Failure( error ); |
| | | 80 | | |
| | | 81 | | /// <summary>Wraps errors as a failed response.</summary> |
| | | 82 | | /// <param name="errors">The errors; at least one.</param> |
| | 0 | 83 | | public static implicit operator LayerResponse( List<Error> errors ) => Failure( errors ); |
| | | 84 | | |
| | | 85 | | /// <summary>Gets the first error, or <see cref="Error.None"/> on success.</summary> |
| | | 86 | | /// <param name="response">The response.</param> |
| | 4 | 87 | | public static implicit operator Error( LayerResponse response ) => response.Errors.FirstOrDefault() ?? Error.None; |
| | | 88 | | |
| | | 89 | | /// <summary>Gets the errors.</summary> |
| | | 90 | | /// <param name="response">The response.</param> |
| | 0 | 91 | | public static implicit operator List<Error>( LayerResponse response ) => response.Errors; |
| | | 92 | | } |