mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-28 09:47:35 +08:00
NoteLoader: Handle the exceptions while trying to load the file
They were being silently dropped.
This commit is contained in:
@ -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";
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Reference in New Issue
Block a user