Skip to content

Commit

Permalink
RailwayStep logging improved. (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-keaton authored Oct 15, 2024
1 parent 71fe116 commit 52cd733
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ChainSharp/Step/IStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public interface IStep<TIn, TOut>
{
public Task<TOut> Run(TIn input);

public Task<Either<Exception, TOut>> RailwayStep(Either<Exception, TIn> previousStep);
public Task<Either<Exception, TOut>> RailwayStep(Either<Exception, TIn> previousOutput);
}
19 changes: 12 additions & 7 deletions ChainSharp/Step/Step.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ public abstract class Step<TIn, TOut> : IStep<TIn, TOut>
{
public abstract Task<TOut> Run(TIn input);

public async Task<Either<Exception, TOut>> RailwayStep(Either<Exception, TIn> previousStep)
public async Task<Either<Exception, TOut>> RailwayStep(Either<Exception, TIn> previousOutput)
{
if (previousStep.IsLeft)
return previousStep.Swap().ValueUnsafe();
if (previousOutput.IsLeft)
return previousOutput.Swap().ValueUnsafe();

Console.WriteLine($"Running Step ({previousStep.GetUnderlyingRightType().Name})");
var stepName = GetType().Name;
var previousOutputName = previousOutput.GetUnderlyingRightType().Name;

Console.WriteLine(
$"Running Step ({stepName}). Previous Output Type ({previousOutputName})"
);

try
{
return await Run(previousStep.ValueUnsafe());
return await Run(previousOutput.ValueUnsafe());
}
catch (Exception e)
{
Expand All @@ -29,10 +34,10 @@ public async Task<Either<Exception, TOut>> RailwayStep(Either<Exception, TIn> pr
if (messageField != null)
messageField.SetValue(
e,
$"{{ \"Step\": \"{GetType().Name}\", \"Type\": \"{e.GetType().Name}\", \"Message\": \"{e.Message}\" }}"
$"{{ \"step\": \"{stepName}\", \"type\": \"{e.GetType().Name}\", \"message\": \"{e.Message}\" }}"
);

Console.WriteLine($"Step: ({GetType().Name}) failed with Exception: ({e.Message})");
Console.WriteLine($"Step: ({stepName}) failed with Exception: ({e.Message})");
return e;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
</PropertyGroup>
</Project>

0 comments on commit 52cd733

Please sign in to comment.