documentation wip
This commit is contained in:
40
MaybeError/Errors/Error.cs
Normal file
40
MaybeError/Errors/Error.cs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MaybeError.Errors;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// An information about an error that occured
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message">Message</param>
|
||||||
|
/// <param name="details">Detailed information of the error</param>
|
||||||
|
/// <param name="devDetails">Even more detailed information that is useful for logging</param>
|
||||||
|
public class Error(string message, string? details = null, string? devDetails = null)
|
||||||
|
{
|
||||||
|
public readonly string Message = message;
|
||||||
|
public readonly string? Details = details;
|
||||||
|
public readonly string? DevDetails = devDetails;
|
||||||
|
|
||||||
|
public virtual Exception GetException()
|
||||||
|
{
|
||||||
|
return new Exception(Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
#if DEBUG
|
||||||
|
return $"{Message}\n{Details}\n{DevDetails}";
|
||||||
|
#else
|
||||||
|
return $"{Message}\n{Details}";
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator string(Error e)
|
||||||
|
{
|
||||||
|
return e.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
36
MaybeError/Errors/ExceptionError.cs
Normal file
36
MaybeError/Errors/ExceptionError.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
namespace MaybeError.Errors;
|
||||||
|
|
||||||
|
public class ExceptionError : ExceptionError<Exception>
|
||||||
|
{
|
||||||
|
public ExceptionError(string message, Exception exception) : base(message, exception)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
public ExceptionError(Exception exception) : base(exception)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ExceptionError<T> : Error where T : Exception
|
||||||
|
{
|
||||||
|
public T Exception { get; set; }
|
||||||
|
|
||||||
|
public ExceptionError(string message, T exception) : base(message, exception.Message, exception.StackTrace)
|
||||||
|
{
|
||||||
|
Exception = exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExceptionError(T exception) : base(exception.Message, devDetails: exception.StackTrace)
|
||||||
|
{
|
||||||
|
Exception = exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override T GetException()
|
||||||
|
{
|
||||||
|
return Exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"{Message}\n{Exception}";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
using MaybeError.Errors;
|
||||||
|
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
namespace MaybeError;
|
namespace MaybeError;
|
||||||
|
|
||||||
@@ -48,23 +50,31 @@ public readonly struct Maybe<T, E> : IMaybe<T, E> where T : class where E: Error
|
|||||||
{
|
{
|
||||||
[MemberNotNullWhen(true, nameof(Error))]
|
[MemberNotNullWhen(true, nameof(Error))]
|
||||||
public readonly bool HasError { get; }
|
public readonly bool HasError { get; }
|
||||||
public readonly bool HasValue { get; }
|
public readonly bool HasValue => !HasError;
|
||||||
public readonly E? Error { get; }
|
public readonly E? Error { get; }
|
||||||
public readonly T Value => HasError ? throw Error.GetException() : _value!;
|
public readonly T Value => HasError ? throw Error.GetException() : _value!;
|
||||||
|
|
||||||
private readonly T? _value;
|
private readonly T? _value;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="Maybe{T, E}"/> with a default(null) value
|
||||||
|
/// </summary>
|
||||||
public Maybe()
|
public Maybe()
|
||||||
{
|
{
|
||||||
_value = default;
|
_value = default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="Maybe{T, E}"/> with a <paramref name="value"/>
|
||||||
|
/// </summary>
|
||||||
public Maybe(T value)
|
public Maybe(T value)
|
||||||
{
|
{
|
||||||
_value = value;
|
_value = value;
|
||||||
HasValue = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="Maybe{T, E}"/> with an error <paramref name="e"/>
|
||||||
|
/// </summary>
|
||||||
public Maybe(E e)
|
public Maybe(E e)
|
||||||
{
|
{
|
||||||
Error = e;
|
Error = e;
|
||||||
@@ -95,23 +105,31 @@ public readonly struct Maybe<T> : IMaybe<T, Error> where T : class
|
|||||||
{
|
{
|
||||||
[MemberNotNullWhen(true, nameof(Error))]
|
[MemberNotNullWhen(true, nameof(Error))]
|
||||||
public readonly bool HasError { get; }
|
public readonly bool HasError { get; }
|
||||||
public readonly bool HasValue { get; }
|
public readonly bool HasValue => !HasError;
|
||||||
public readonly Error? Error { get; }
|
public readonly Error? Error { get; }
|
||||||
public readonly T Value => HasError ? throw Error.GetException() : _value!;
|
public readonly T Value => HasError ? throw Error.GetException() : _value!;
|
||||||
|
|
||||||
private readonly T? _value;
|
private readonly T? _value;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="Maybe{T}"/> with a default(null) value
|
||||||
|
/// </summary>
|
||||||
public Maybe()
|
public Maybe()
|
||||||
{
|
{
|
||||||
_value = default;
|
_value = default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="Maybe{T}"/> with a <paramref name="value"/>
|
||||||
|
/// </summary>
|
||||||
public Maybe(T value)
|
public Maybe(T value)
|
||||||
{
|
{
|
||||||
_value = value;
|
_value = value;
|
||||||
HasValue = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="Maybe{T}"/> with an exception <paramref name="e"/>
|
||||||
|
/// </summary>
|
||||||
public Maybe(Exception e)
|
public Maybe(Exception e)
|
||||||
{
|
{
|
||||||
Error = new ExceptionError(e);
|
Error = new ExceptionError(e);
|
||||||
@@ -119,6 +137,9 @@ public readonly struct Maybe<T> : IMaybe<T, Error> where T : class
|
|||||||
_value = default;
|
_value = default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="Maybe{T}"/> with an error <paramref name="e"/>
|
||||||
|
/// </summary>
|
||||||
public Maybe(Error e)
|
public Maybe(Error e)
|
||||||
{
|
{
|
||||||
Error = e;
|
Error = e;
|
||||||
@@ -151,58 +172,3 @@ public readonly struct Maybe<T> : IMaybe<T, Error> where T : class
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Error(string message, string? details = null, string? debugDetails = null)
|
|
||||||
{
|
|
||||||
public readonly string Message = message;
|
|
||||||
public readonly string? Details = details;
|
|
||||||
public readonly string? DebugDetails = debugDetails;
|
|
||||||
|
|
||||||
public virtual Exception GetException()
|
|
||||||
{
|
|
||||||
return new Exception(Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return $"{Message}\n{Details}\n{DebugDetails}";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static implicit operator string(Error e)
|
|
||||||
{
|
|
||||||
return e.ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class ExceptionError : ExceptionError<Exception>
|
|
||||||
{
|
|
||||||
public ExceptionError(string message, Exception exception) : base(message, exception)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
public ExceptionError(Exception exception) : base(exception)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public class ExceptionError<T> : Error where T : Exception
|
|
||||||
{
|
|
||||||
public T Exception { get; set; }
|
|
||||||
|
|
||||||
public ExceptionError(string message, T exception) : base(message, exception.Message, exception.StackTrace)
|
|
||||||
{
|
|
||||||
Exception = exception;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExceptionError(T exception) : base(exception.Message, debugDetails: exception.StackTrace)
|
|
||||||
{
|
|
||||||
Exception = exception;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override T GetException()
|
|
||||||
{
|
|
||||||
return Exception;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return $"{Message}\n{Exception}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#nullable enable
|
using MaybeError.Errors;
|
||||||
|
|
||||||
namespace MaybeError;
|
namespace MaybeError;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
using MaybeError.Errors;
|
||||||
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
namespace MaybeError;
|
namespace MaybeError;
|
||||||
@@ -22,17 +24,26 @@ public readonly struct ValueMaybe<T, E> : IValueMaybe<T, E> where T : struct whe
|
|||||||
|
|
||||||
private readonly T _value;
|
private readonly T _value;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="ValueMaybe{T, E}"/> with a default value
|
||||||
|
/// </summary>
|
||||||
public ValueMaybe()
|
public ValueMaybe()
|
||||||
{
|
{
|
||||||
_value = default;
|
_value = default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="ValueMaybe{T, E}"/> with a <paramref name="value"/>
|
||||||
|
/// </summary>
|
||||||
public ValueMaybe(T value)
|
public ValueMaybe(T value)
|
||||||
{
|
{
|
||||||
_value = value;
|
_value = value;
|
||||||
HasError = false;
|
HasError = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="ValueMaybe{T, E}"/> with an error <paramref name="e"/>
|
||||||
|
/// </summary>
|
||||||
public ValueMaybe(E e)
|
public ValueMaybe(E e)
|
||||||
{
|
{
|
||||||
Error = e;
|
Error = e;
|
||||||
@@ -68,17 +79,26 @@ public readonly struct ValueMaybe<T> : IValueMaybe<T, Error> where T : struct
|
|||||||
|
|
||||||
private readonly T _value;
|
private readonly T _value;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="ValueMaybe{T}"/> with a default value
|
||||||
|
/// </summary>
|
||||||
public ValueMaybe()
|
public ValueMaybe()
|
||||||
{
|
{
|
||||||
_value = default;
|
_value = default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="ValueMaybe{T}"/> with a <paramref name="value"/>
|
||||||
|
/// </summary>
|
||||||
public ValueMaybe(T value)
|
public ValueMaybe(T value)
|
||||||
{
|
{
|
||||||
_value = value;
|
_value = value;
|
||||||
HasError = false;
|
HasError = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="ValueMaybe{T}"/> with an exception <paramref name="e"/>
|
||||||
|
/// </summary>
|
||||||
public ValueMaybe(Exception e)
|
public ValueMaybe(Exception e)
|
||||||
{
|
{
|
||||||
Error = new ExceptionError(e);
|
Error = new ExceptionError(e);
|
||||||
@@ -86,6 +106,9 @@ public readonly struct ValueMaybe<T> : IValueMaybe<T, Error> where T : struct
|
|||||||
_value = default;
|
_value = default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="ValueMaybe{T}"/> with an error <paramref name="e"/>
|
||||||
|
/// </summary>
|
||||||
public ValueMaybe(Error e)
|
public ValueMaybe(Error e)
|
||||||
{
|
{
|
||||||
Error = e;
|
Error = e;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using MaybeError;
|
using MaybeError;
|
||||||
|
using MaybeError.Errors;
|
||||||
|
|
||||||
namespace MaybeErrorTests;
|
namespace MaybeErrorTests;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using MaybeError;
|
using MaybeError;
|
||||||
|
using MaybeError.Errors;
|
||||||
|
|
||||||
namespace MaybeErrorTests;
|
namespace MaybeErrorTests;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user