LCOV - code coverage report

Current view
top level - /src - squadron_exception.dart
Test
lcov.info
Date
2022-04-02
Legend
Lines
hit
not hit
Branches
taken
not taken
# not executed
HitTotalCoverage
Lines303781.1%
Functions00-
Branches00-
Each row represents a line of source code
LineBranchHitsSource code
1import 'dart:convert';
2
3import 'squadron_error.dart';
4import 'worker_exception.dart';
5
6typedef WorkerExceptionDeserializer = WorkerException? Function(List data);
7
8/// Base abstract class for exceptions in Squadron.
9abstract class SquadronException implements Exception {
10 /// This factory returns [error] if it is a [SquadronException] (enriching it with [workerId] and [command] if it
11 /// is a [WorkerException]). Otherwise, it returns a new [WorkerException] wrapping [error] and [stackTrace].
122 factory SquadronException.from(
13 {required Object error,
14 StackTrace? stackTrace,
15 String? workerId,
16 int? command}) {
172 if (error is SquadronError) {
180 return error;
191 } else if (error is WorkerException) {
203 return error.withCommand(command).withWorkerId(workerId);
21 } else {
223 return WorkerException(error.toString(),
23 stackTrace: stackTrace, workerId: workerId, command: command);
24 }
251 }
26
271 static SquadronException? fromString(String message) {
28 try {
291 final data = jsonDecode(message);
301 if (data is List) {
311 return deserialize(data);
32 }
33 } catch (ex) {
34 // not a String representing a SquadronException
35 }
36 return null;
37 }
38
390 @override
400 String toString() => jsonEncode(serialize());
41
42 /// The exception's [StackTrace].
43 StackTrace? get stackTrace;
44
45 /// Serializes the exception, i.e. returns a list of items that can cross thread boundaries.
46 List serialize();
47
483 static final _customDeserializers = <WorkerExceptionDeserializer>[];
49
50 /// Registers the deserializer for a custom [WorkerException].
511 static void registerExceptionDeserializer(
52 WorkerExceptionDeserializer deserializer) {
533 _customDeserializers.add(deserializer);
54 }
55
56 /// Deserializes a [stackTrace] if any. Ruturns null if no [StackTrace] is provided.
571 static StackTrace? loadStackTrace(String? stackTrace) =>
581 (stackTrace == null) ? null : StackTrace.fromString(stackTrace);
59
60 /// Deserializes a [List] that was produced by [serialize].
612 static SquadronException? deserialize(List? data) {
621 if (data == null) {
631 return null;
64 }
65 SquadronException? error;
660 try {
672 error = SquadronError.deserialize(data) ??
681 WorkerException.deserialize(data) ??
691 CancelledException.deserialize(data) ??
701 TaskTimeoutException.deserialize(data);
711 if (error == null) {
724 for (var i = 0; i < _customDeserializers.length; i++) {
733 final deserializer = _customDeserializers[i];
742 error = deserializer(data);
751 if (error != null) {
761 break;
77 }
78 }
79 }
800 error ??= newSquadronError(
810 'failed to deserialize exception information: $data');
82 } catch (ex) {
83 error =
840 newSquadronError('failed to deserialize exception information: $ex');
85 }
861 return error;
871 }
88}
Choose Features