rethrowOrFormatException function
Main-side: if error is a decode-failure signal (a StateError whose
message starts with decodeFailurePrefix, as produced by throwDecodeFailure
in the isolate and re-wrapped by IsolateRpcClient), throw a
FormatException carrying the detail and optional source bytes; otherwise
rethrow error unchanged.
try {
result = await worker.sendRequest('detect', params);
} catch (e) {
rethrowOrFormatException(e, imageBytes);
}
Implementation
Never rethrowOrFormatException(Object error, [Uint8List? source]) {
final String msg = error is StateError ? error.message : error.toString();
if (msg.startsWith(decodeFailurePrefix)) {
final String detail = msg.substring(decodeFailurePrefix.length).trim();
throw FormatException(
detail.isEmpty ? 'Image bytes could not be decoded.' : detail,
source,
);
}
throw error;
}