NoteLoader: Handle the exceptions while trying to load the file

They were being silently dropped.
This commit is contained in:
Vishesh Handa
2020-09-03 13:43:33 +02:00
parent ec91a784de
commit d3d04e6df9
2 changed files with 45 additions and 7 deletions

View File

@ -22,7 +22,11 @@ class MdYamlDocLoader {
_isolate.kill(priority: Isolate.immediate);
_isolate = null;
}
_isolate = await Isolate.spawn(_isolateMain, _receivePort.sendPort);
_isolate = await Isolate.spawn(
_isolateMain,
_receivePort.sendPort,
errorsAreFatal: false,
);
var data = await _receivePort.first;
assert(data is SendPort);
@ -42,9 +46,15 @@ class MdYamlDocLoader {
_sendPort.send(_LoadingMessage(filePath, rec.sendPort));
var data = await rec.first;
assert(data is MdYamlDoc);
assert(data is _LoaderResponse);
var resp = data as _LoaderResponse;
assert(resp.filePath == filePath);
return data;
if (resp.doc != null) {
return resp.doc;
}
throw MdYamlParsingException(filePath, resp.err.toString());
}
}
@ -65,14 +75,26 @@ void _isolateMain(SendPort toMainSender) {
assert(data is _LoadingMessage);
var msg = data as _LoadingMessage;
final file = File(msg.filePath);
final fileData = await file.readAsString();
var doc = _serializer.decode(fileData);
try {
final file = File(msg.filePath);
final fileData = await file.readAsString();
var doc = _serializer.decode(fileData);
msg.sendPort.send(doc);
msg.sendPort.send(_LoaderResponse(msg.filePath, doc));
} catch (err) {
msg.sendPort.send(_LoaderResponse(msg.filePath, null, err.toString()));
}
});
}
class _LoaderResponse {
final String filePath;
final MdYamlDoc doc;
final String err;
_LoaderResponse(this.filePath, this.doc, [this.err]);
}
class MdYamlDocNotFoundException implements Exception {
final String filePath;
MdYamlDocNotFoundException(this.filePath);
@ -80,3 +102,13 @@ class MdYamlDocNotFoundException implements Exception {
@override
String toString() => "MdYamlDocNotFoundException: $filePath";
}
class MdYamlParsingException implements Exception {
final String filePath;
final String error;
MdYamlParsingException(this.filePath, this.error);
@override
String toString() => "MdYamlParsingException: $filePath - $error";
}

View File

@ -301,6 +301,12 @@ class Note with NotesNotifier {
_loadState = NoteLoadState.NotExists;
_notifyModified();
return _loadState;
} on MdYamlParsingException catch (err, stackTrace) {
logException(err, stackTrace);
_loadState = NoteLoadState.Error;
_notifyModified();
return _loadState;
}
} else if (isTxt) {
try {