Skip to content

Commit c8fa65f

Browse files
authored
Merge pull request #11 from catcherwong/iss-10-optimize-callbranch
fix: optimize callbranch with exception logic
2 parents 1fbb384 + 868ad33 commit c8fa65f

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

src/Dtmcli/Constant.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ internal class Constant
2727
/// </summary>
2828
internal static readonly string StatusAborting = "aborting";
2929

30-
3130
/// <summary>
3231
/// branch type for TCC
3332
/// </summary>
@@ -53,6 +52,10 @@ internal class Constant
5352
/// </summary>
5453
internal static readonly string BranchRollback = "rollback";
5554

55+
internal static readonly string ErrFailure = "FAILRUE";
56+
57+
internal static readonly int FailureStatusCode = 400;
58+
5659
internal class Request
5760
{
5861
internal static readonly string CONTENT_TYPE = "application/json";

src/Dtmcli/DtmcliException.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace Dtmcli
4+
{
5+
public class DtmcliException : Exception
6+
{
7+
public DtmcliException(string message)
8+
: base(message)
9+
{
10+
}
11+
}
12+
}

src/Dtmcli/Tcc/Tcc.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,19 @@ public async Task<string> CallBranch(object body, string tryUrl, string confirmU
3535
Constant.BranchTry,
3636
tryUrl,
3737
cancellationToken).ConfigureAwait(false);
38-
39-
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
38+
39+
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
40+
41+
// call branch error should throw exception
42+
var isOldVerException = response.StatusCode == System.Net.HttpStatusCode.OK && content.Contains(Constant.ErrFailure);
43+
var isNewVerException = (int)response.StatusCode >= Constant.FailureStatusCode;
44+
45+
if (isOldVerException || isNewVerException)
46+
{
47+
throw new DtmcliException("An exception occurred when CallBranch");
48+
}
49+
50+
return content;
4051
}
4152

4253
public DtmImp.TransBase GetTransBase() => _transBase;

0 commit comments

Comments
 (0)