mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 18:38:36 +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.kill(priority: Isolate.immediate);
|
||||||
_isolate = null;
|
_isolate = null;
|
||||||
}
|
}
|
||||||
_isolate = await Isolate.spawn(_isolateMain, _receivePort.sendPort);
|
_isolate = await Isolate.spawn(
|
||||||
|
_isolateMain,
|
||||||
|
_receivePort.sendPort,
|
||||||
|
errorsAreFatal: false,
|
||||||
|
);
|
||||||
|
|
||||||
var data = await _receivePort.first;
|
var data = await _receivePort.first;
|
||||||
assert(data is SendPort);
|
assert(data is SendPort);
|
||||||
@ -42,9 +46,15 @@ class MdYamlDocLoader {
|
|||||||
_sendPort.send(_LoadingMessage(filePath, rec.sendPort));
|
_sendPort.send(_LoadingMessage(filePath, rec.sendPort));
|
||||||
|
|
||||||
var data = await rec.first;
|
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);
|
assert(data is _LoadingMessage);
|
||||||
var msg = data as _LoadingMessage;
|
var msg = data as _LoadingMessage;
|
||||||
|
|
||||||
|
try {
|
||||||
final file = File(msg.filePath);
|
final file = File(msg.filePath);
|
||||||
final fileData = await file.readAsString();
|
final fileData = await file.readAsString();
|
||||||
var doc = _serializer.decode(fileData);
|
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 {
|
class MdYamlDocNotFoundException implements Exception {
|
||||||
final String filePath;
|
final String filePath;
|
||||||
MdYamlDocNotFoundException(this.filePath);
|
MdYamlDocNotFoundException(this.filePath);
|
||||||
@ -80,3 +102,13 @@ class MdYamlDocNotFoundException implements Exception {
|
|||||||
@override
|
@override
|
||||||
String toString() => "MdYamlDocNotFoundException: $filePath";
|
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;
|
_loadState = NoteLoadState.NotExists;
|
||||||
_notifyModified();
|
_notifyModified();
|
||||||
return _loadState;
|
return _loadState;
|
||||||
|
} on MdYamlParsingException catch (err, stackTrace) {
|
||||||
|
logException(err, stackTrace);
|
||||||
|
|
||||||
|
_loadState = NoteLoadState.Error;
|
||||||
|
_notifyModified();
|
||||||
|
return _loadState;
|
||||||
}
|
}
|
||||||
} else if (isTxt) {
|
} else if (isTxt) {
|
||||||
try {
|
try {
|
||||||
|
Reference in New Issue
Block a user