Skip to content

Commit

Permalink
Fix for 'v5 - Use custom sub-type of Expected losses all custom prope…
Browse files Browse the repository at this point in the history
…rties after RunAsync

#1340'

#1340
  • Loading branch information
louthy committed Jul 28, 2024
1 parent c4b25f9 commit ea389dd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
6 changes: 3 additions & 3 deletions LanguageExt.Core/Common/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public override string ToString() =>
/// </summary>
[Pure]
public override ErrorException ToErrorException() =>
new ExpectedException(Message, Code, Inner.Map(static e => e.ToErrorException()));
new WrappedErrorExpectedException(this);

/// <summary>
/// Returns false because this type isn't exceptional
Expand Down Expand Up @@ -482,9 +482,9 @@ public override Exception ToException() =>
/// </summary>
/// <returns></returns>
[Pure]
public override ErrorException ToErrorException() =>
public override ErrorException ToErrorException() =>
Value == null
? new ExceptionalException(Message, Code)
? new WrappedErrorExceptionalException(this)
: new ExceptionalException(Value);

/// <summary>
Expand Down
24 changes: 23 additions & 1 deletion LanguageExt.Core/Common/ErrorException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public static ErrorException Many(Seq<ErrorException> errors) =>
/// <summary>
/// Represents expected errors
/// </summary>
public sealed class ExpectedException : ErrorException
public class ExpectedException : ErrorException
{
public ExpectedException(string message, int code, Option<ErrorException> inner) : base(code) =>
(Message, Code, Inner) = (message, code, inner);
Expand Down Expand Up @@ -232,6 +232,17 @@ error is ManyExceptions m
: new ManyExceptions(Seq(this, error));
}

/// <summary>
/// Wraps an `Error` maintaining its type for subsequent conversion back to an `Error` later
/// </summary>
/// <param name="Error"></param>
public sealed class WrappedErrorExpectedException(Error Error) :
ExpectedException(Error.Message, Error.Code, Error.Inner.Map(e => e.ToErrorException()))
{
public override Error ToError() =>
Error;
}

/// <summary>
/// Represents an exceptional (unexpected) error
/// </summary>
Expand Down Expand Up @@ -306,6 +317,17 @@ error is ManyExceptions m
: new ManyExceptions(Seq(this, error));
}

/// <summary>
/// Wraps an `Error` maintaining its type for subsequent conversion back to an `Error` later
/// </summary>
/// <param name="Error"></param>
public sealed class WrappedErrorExceptionalException(Error Error) :
ExceptionalException(Error.Message, Error.Code)
{
public override Error ToError() =>
Error;
}

/// <summary>
/// Represents multiple errors
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,4 @@ static K<ReaderT<Env, M>, B> Monad<ReaderT<Env, M>>.WithRunInIO<A, B>(
static K<ReaderT<Env, M>, A> SemigroupK<ReaderT<Env, M>>.Combine<A>(
K<ReaderT<Env, M>, A> ma, K<ReaderT<Env, M>, A> mb) =>
new ReaderT<Env, M, A>(env => M.Combine(ma.As().runReader(env), mb.As().runReader(env)));

}
31 changes: 31 additions & 0 deletions LanguageExt.Tests/IssuesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Newtonsoft.Json;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using LanguageExt.Common;
using static LanguageExt.Prelude;

namespace LanguageExt.Tests
{
Expand Down Expand Up @@ -425,4 +427,33 @@ from r in ExecuteCommand(f, cmd)
.Traverse(x => x).As();
}
}


public class Issue1340
{
public sealed record CustomExpected(string Message, int Code, string Another) : Expected(Message, Code);

[Fact]
public void Test()
{
// Arrange
var expected = new CustomExpected("Name", 100, "This is loss");
Eff<int> effect = liftEff<int>(() => expected);

// Act
Fin<int> fin = effect.Run();

// Assert
fin.Match(_ => Assert.True(false),
error =>
{
Assert.Equal(error.Code, expected.Code);
Assert.Equal(error.Message, expected.Message);
var fail = (CustomExpected)error;
Assert.Equal(fail.Another, expected.Another);
});

}
}
}

0 comments on commit ea389dd

Please sign in to comment.