Skip to content

Commit f9010a6

Browse files
committed
handle Exception during Execution
1 parent e3cb863 commit f9010a6

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

PipelineBlocks/Models/BlockResult.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,34 @@ public class BlockResult
55
public string? Key { get; }
66
public object? Data { get; }
77
public string? Message { get; }
8+
public Exception? Exception { get; set; }
89
public BlockResultType ResultType { get; }
910

10-
internal BlockResult(object? data, string? key, string? message, BlockResultType resultType)
11+
internal BlockResult(object? data, string? key, string? message, BlockResultType resultType, Exception? exception)
1112
{
1213
Data = data;
1314
Key = key;
1415
Message = message;
1516
ResultType = resultType;
17+
Exception = exception;
1618
}
1719

18-
public static BlockResult Completed(string? message = null) => new(default, null, message, BlockResultType.Completed);
19-
public static BlockResult Error(string? message = null) => new(default, null, message, BlockResultType.Error);
20-
public static BlockResult<T> Exit<T>(T? data = default) => new(data, null, null, BlockResultType.Exit);
21-
public static BlockResult<T> Forward<T>(T? data = default) => new(data, null, null, BlockResultType.Forward);
22-
public static BlockResult<T> Execute<T>(T? data = default) => new(data, null, null, BlockResultType.Execute);
20+
public static BlockResult Completed(string? message = null) => new(default, null, message, BlockResultType.Completed, null);
21+
public static BlockResult Error(string? message = null, Exception? exception = null) => new(default, null, message, BlockResultType.Error, exception);
22+
public static BlockResult<T> Exit<T>(T? data = default) => new(data, null, null, BlockResultType.Exit, null);
23+
public static BlockResult<T> Forward<T>(T? data = default) => new(data, null, null, BlockResultType.Forward, null);
24+
public static BlockResult<T> Execute<T>(T? data = default) => new(data, null, null, BlockResultType.Execute, null);
2325
}
2426

2527
public class BlockResult<T> : BlockResult
2628
{
2729
public new T? Data { get; }
2830

29-
internal BlockResult(T? data, string? key, string? message, BlockResultType resultType) : base(data, key, message, resultType) => Data = data;
31+
internal BlockResult(T? data, string? key, string? message, BlockResultType resultType, Exception? exception) : base(data, key, message, resultType, exception) => Data = data;
3032

31-
public static new BlockResult<T> Completed(string? message = null) => new(default, null, message, BlockResultType.Completed);
32-
public static BlockResult<T> Skip() => new(default, null, null, BlockResultType.Skip);
33-
public static new BlockResult<T> Error(string? message = null) => new(default, null, message, BlockResultType.Error);
34-
public static BlockResult<T> BackToCheckpoint(string? key = null) => new(default, key, null, BlockResultType.BackToCheckpoint);
35-
public static BlockResult<T> BackToExit(string? key = null) => new(default, key, null, BlockResultType.BackToExit);
33+
public static new BlockResult<T> Completed(string? message = null) => new(default, null, message, BlockResultType.Completed, null);
34+
public static BlockResult<T> Skip() => new(default, null, null, BlockResultType.Skip, null);
35+
public static new BlockResult<T> Error(string? message = null, Exception? exception = null) => new(default, null, message, BlockResultType.Error, exception);
36+
public static BlockResult<T> BackToCheckpoint(string? key = null) => new(default, key, null, BlockResultType.BackToCheckpoint, null);
37+
public static BlockResult<T> BackToExit(string? key = null) => new(default, key, null, BlockResultType.BackToExit, null);
3638
}

PipelineBlocks/Models/PipelineBlock.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,25 @@ async Task<BlockResult> IExecutableBlock.ExecuteSelfAsync(CancellationToken canc
3333
IsCompleted = true;
3434
return BlockResult.Error("No job");
3535
}
36-
BlockResult<T>? result = await Job.Invoke(this, cancellationToken);
37-
return result is null
38-
? BlockResult.Error("Job returned null")
39-
: result.ResultType switch
40-
{
41-
BlockResultType.Exit or BlockResultType.Completed => Exit(result),
42-
BlockResultType.Forward => Forward(result),
43-
BlockResultType.BackToCheckpoint => BackToCheckpoint(result),
44-
BlockResultType.BackToExit => BackToExit(result),
45-
BlockResultType.Skip => Skip(),
46-
_ => result
47-
};
36+
try
37+
{
38+
BlockResult<T>? result = await Job.Invoke(this, cancellationToken);
39+
return result is null
40+
? BlockResult.Error("Job returned null")
41+
: result.ResultType switch
42+
{
43+
BlockResultType.Exit or BlockResultType.Completed => Exit(result),
44+
BlockResultType.Forward => Forward(result),
45+
BlockResultType.BackToCheckpoint => BackToCheckpoint(result),
46+
BlockResultType.BackToExit => BackToExit(result),
47+
BlockResultType.Skip => Skip(),
48+
_ => result
49+
};
50+
}
51+
catch (Exception ex)
52+
{
53+
return BlockResult.Error("Job execution failed", ex);
54+
}
4855
}
4956

5057
public async Task<BlockResult> ExecuteAsync(CancellationToken cancellationToken = default)

PipelineBlocksTests/Models/PipelineBlockTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,21 @@ public async Task ExecuteAsync_HasJob_ShouldReturnTrue()
175175
(await act.Should().NotThrowAsync()).Which.ResultType.Should().Be(BlockResultType.Completed);
176176
}
177177

178+
[TestMethod()]
179+
public async Task ExecuteAsync_Exception_ShouldReturnError()
180+
{
181+
// arrange
182+
var exception = new Exception("Job execution failed");
183+
PipelineBlock<int> block = new()
184+
{
185+
Job = (x, _) => throw exception
186+
};
187+
// act
188+
Func<Task<BlockResult>> act = () => block.ExecuteAsync();
189+
// assert
190+
(await act.Should().NotThrowAsync()).Which.Should().BeEquivalentTo(BlockResult.Error("Job execution failed", exception));
191+
}
192+
178193
[TestMethod()]
179194
public void IsCompleted_NotExecuted_ShouldReturnFalse()
180195
{

0 commit comments

Comments
 (0)