1 | | | import 'dart:async'; |
2 | | |
|
3 | | | import 'squadron_exception.dart'; |
4 | | |
|
5 | | | /// Exception to keep track of errors encountered in a worker. |
6 | | | class WorkerException implements SquadronException { |
7 | | | /// Creates a new [WorkerException] to capture error context. |
8 | | 2 | WorkerException(this.message, |
9 | | | {StackTrace? stackTrace, String? workerId, int? command}) |
10 | | 2 | : _stackTrace = stackTrace ?? StackTrace.current, |
11 | | | _workerId = workerId, |
12 | | 1 | _command = command; |
13 | | |
|
14 | | | static const _$type = 0; |
15 | | | static const _$message = 1; |
16 | | | static const _$stackTrace = 2; |
17 | | | static const _$workerId = 3; |
18 | | | static const _$command = 4; |
19 | | |
|
20 | | | static const _$typeMarker = '\$W'; |
21 | | |
|
22 | | 1 | @override |
23 | | 1 | List serialize() => |
24 | | 7 | [_$typeMarker, message, _stackTrace?.toString(), _workerId, _command]; |
25 | | |
|
26 | | 1 | static WorkerException? deserialize(List data) => |
27 | | 3 | (data[_$type] == _$typeMarker) |
28 | | 3 | ? WorkerException(data[_$message], |
29 | | 3 | stackTrace: SquadronException.loadStackTrace(data[_$stackTrace]), |
30 | | 2 | workerId: data[_$workerId], |
31 | | 2 | command: data[_$command]) |
32 | | | : null; |
33 | | |
|
34 | | | /// Message. |
35 | | | final String message; |
36 | | |
|
37 | | | /// Stack trace associated to the original exception. |
38 | | 1 | @override |
39 | | 2 | StackTrace? get stackTrace => _stackTrace; |
40 | | | StackTrace? _stackTrace; |
41 | | |
|
42 | | | /// Worker ID. |
43 | | 2 | String? get workerId => _workerId; |
44 | | | String? _workerId; |
45 | | |
|
46 | | | /// Command. |
47 | | 2 | int? get command => _command; |
48 | | | int? _command; |
49 | | | } |
50 | | |
|
51 | | | /// Exception to keep track of task cancellation. |
52 | | | class CancelledException extends WorkerException { |
53 | | 2 | CancelledException( |
54 | | | {String? message, StackTrace? stackTrace, String? workerId, int? command}) |
55 | | 1 | : super(message ?? 'The task has been cancelled', |
56 | | 1 | stackTrace: stackTrace, workerId: workerId, command: command); |
57 | | |
|
58 | | | static const _$type = 0; |
59 | | | static const _$message = 1; |
60 | | | static const _$stackTrace = 2; |
61 | | | static const _$workerId = 3; |
62 | | | static const _$command = 4; |
63 | | |
|
64 | | | static const _$typeMarker = '\$C'; |
65 | | |
|
66 | | 1 | @override |
67 | | 0 | List serialize() => |
68 | | 6 | [_$typeMarker, message, _stackTrace?.toString(), _workerId, _command]; |
69 | | |
|
70 | | 1 | static CancelledException? deserialize(List data) => |
71 | | 3 | (data[_$type] == _$typeMarker) |
72 | | 2 | ? CancelledException( |
73 | | 2 | message: data[_$message], |
74 | | 3 | stackTrace: SquadronException.loadStackTrace(data[_$stackTrace]), |
75 | | 2 | workerId: data[_$workerId], |
76 | | 2 | command: data[_$command]) |
77 | | | : null; |
78 | | | } |
79 | | |
|
80 | | | /// Exception to keep track of task timeouts. |
81 | | | class TaskTimeoutException extends CancelledException |
82 | | | implements TimeoutException { |
83 | | | /// Creates a new [TaskTimeoutException]. |
84 | | 2 | TaskTimeoutException( |
85 | | | {String? message, |
86 | | | StackTrace? stackTrace, |
87 | | | String? workerId, |
88 | | | int? command, |
89 | | | this.duration}) |
90 | | 1 | : super( |
91 | | | message: message ?? 'The task timed out', |
92 | | | stackTrace: stackTrace, |
93 | | | workerId: workerId, |
94 | | 1 | command: command); |
95 | | |
|
96 | | | static const _$type = 0; |
97 | | | static const _$message = 1; |
98 | | | static const _$stackTrace = 2; |
99 | | | static const _$workerId = 3; |
100 | | | static const _$command = 4; |
101 | | | static const _$duration = 5; |
102 | | |
|
103 | | | static const _$typeMarker = '\$T'; |
104 | | |
|
105 | | 0 | @override |
106 | | 0 | List serialize() => [ |
107 | | | _$typeMarker, |
108 | | 0 | message, |
109 | | 0 | _stackTrace?.toString(), |
110 | | 0 | _workerId, |
111 | | 0 | _command, |
112 | | 0 | duration?.inMicroseconds |
113 | | 0 | ]; |
114 | | |
|
115 | | | @override |
116 | | | final Duration? duration; |
117 | | |
|
118 | | 1 | static TaskTimeoutException? deserialize(List data) => |
119 | | 3 | (data[_$type] == _$typeMarker) |
120 | | 0 | ? TaskTimeoutException( |
121 | | 0 | message: data[_$message], |
122 | | 0 | stackTrace: SquadronException.loadStackTrace(data[_$stackTrace]), |
123 | | 0 | workerId: data[_$workerId], |
124 | | 0 | command: data[_$command], |
125 | | 0 | duration: (data[_$duration] == null) |
126 | | | ? null |
127 | | 0 | : Duration(microseconds: data[_$duration])) |
128 | | | : null; |
129 | | | } |
130 | | |
|
131 | | | // private implementation internal to Squadron |
132 | | | extension WorkerExceptionExt on WorkerException { |
133 | | 2 | WorkerException withWorkerId(String? workerId) { |
134 | | 2 | _workerId ??= workerId; |
135 | | 1 | return this; |
136 | | 1 | } |
137 | | |
|
138 | | 2 | WorkerException withCommand(int? command) { |
139 | | 2 | _command ??= command; |
140 | | 1 | return this; |
141 | | 1 | } |
142 | | |
|
143 | | 0 | WorkerException withStackTrace(StackTrace? stackTrace) { |
144 | | 0 | _stackTrace ??= stackTrace; |
145 | | | return this; |
146 | | | } |
147 | | | } |