forked from louthy/language-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeadLetter.cs
112 lines (97 loc) · 4.21 KB
/
DeadLetter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using static LanguageExt.Prelude;
namespace LanguageExt
{
/// <summary>
/// Dead letter message
/// </summary>
public class DeadLetter
{
private DeadLetter(ProcessId sender, ProcessId recipient, Exception ex, string reason, object message)
{
Sender = sender;
Recipient = recipient;
Exception = Optional(ex);
Reason = Optional(reason);
Message = Optional(message);
}
/// <summary>
/// Create a new dead letter
/// </summary>
public static DeadLetter create(ProcessId sender, ProcessId recipient, Exception ex, string reason, object message) =>
new DeadLetter(sender, recipient, ex, reason, message);
/// <summary>
/// Create a new dead letter
/// </summary>
public static DeadLetter create(ProcessId sender, ProcessId recipient, string reason, object message) =>
new DeadLetter(sender, recipient, null, reason, message);
/// <summary>
/// Create a new dead letter
/// </summary>
public static DeadLetter create(ProcessId sender, ProcessId recipient, Exception ex, object message) =>
new DeadLetter(sender, recipient, ex, null, message);
/// <summary>
/// Create a new dead letter
/// </summary>
public static DeadLetter create(ProcessId sender, ProcessId recipient, object message) =>
new DeadLetter(sender, recipient, null, null, message);
/// <summary>
/// Sender of the letter that ended up 'dead'
/// </summary>
public readonly ProcessId Sender;
/// <summary>
/// Intended recipient of the message that ended up 'dead'
/// </summary>
public readonly ProcessId Recipient;
/// <summary>
/// Any exception that was thrown the cause the letter to die
/// </summary>
public readonly Option<Exception> Exception;
/// <summary>
/// An optional reason why the letter died
/// </summary>
public readonly Option<string> Reason;
/// <summary>
/// The content of the dead letter
/// </summary>
public Option<object> Message;
/// <summary>
/// Summary of the message content
/// </summary>
public string ContentDisplay =>
Message.Match(
Some: objmsg => map(objmsg.ToString(), msg =>
msg.Length > 100
? msg.Substring(0, 100) + "..."
: msg),
None: () => "[null]"
);
/// <summary>
/// Friendly type display
/// </summary>
public string ContentTypeDisplay =>
Message.Match(
Some: x => x.GetType().Name,
None: () => "[null]"
);
private static string ProcessFmt(ProcessId pid) =>
pid.IsValid
? pid.ToString()
: "no-sender";
/// <summary>
/// Get a string representation of the dead letter
/// </summary>
public override string ToString() =>
Exception.Match(
Some: ex =>
Reason.Match(
Some: reason => String.Format("Dead letter from: {0} to: {1}, failed because: {2} {3}. Type: {4} Content: {5}", ProcessFmt(Sender), Recipient, reason, ex.Message, ContentTypeDisplay, ContentDisplay),
None: () => String.Format("Dead letter from: {0} to: {1}, failed because: {2}. Type: {3} Content: {4}", ProcessFmt(Sender), Recipient, ex.Message, ContentTypeDisplay, ContentDisplay)
),
None: () =>
Reason.Match(
Some: reason => String.Format("Dead letter from: {0} to: {1}, failed because: {2}. Type: {3} Content: {4}", ProcessFmt(Sender), Recipient, reason, ContentTypeDisplay, ContentDisplay),
None: () => String.Format("Dead letter from: {0} to: {1}. Type: {2} Content: {3}", ProcessFmt(Sender), Recipient, ContentTypeDisplay, ContentDisplay)
));
}
}