Tweak error handling in collection streams

This commit is contained in:
Ming Ming 2023-04-26 01:08:28 +08:00
parent 3f38efccf3
commit 19de0fa5b6
8 changed files with 27 additions and 29 deletions

View file

@ -106,8 +106,8 @@ class CollectionItemsController {
return;
}
}
ExceptionEvent? error;
ExceptionEvent? error;
final failed = <FileDescriptor>[];
await _mutex.protect(() async {
await AddFileToCollection(_c)(
@ -165,6 +165,7 @@ class CollectionItemsController {
unawaited(_load());
}
});
error?.throwMe();
}
/// Remove list of [items] from [collection]
@ -180,8 +181,8 @@ class CollectionItemsController {
.toList(),
));
}
ExceptionEvent? error;
ExceptionEvent? error;
final failed = <CollectionItem>[];
await _mutex.protect(() async {
await RemoveFromCollection(_c)(
@ -212,6 +213,7 @@ class CollectionItemsController {
unawaited(_load());
}
});
error?.throwMe();
}
/// Delete list of [files] from your server
@ -241,8 +243,8 @@ class CollectionItemsController {
} else {
toDelete = files;
}
ExceptionEvent? error;
ExceptionEvent? error;
final failed = <CollectionItem>[];
await _mutex.protect(() async {
await Remove(_c)(
@ -271,6 +273,7 @@ class CollectionItemsController {
unawaited(_load());
}
});
error?.throwMe();
}
/// Replace items in the stream, for internal use only

View file

@ -4,6 +4,10 @@ class ExceptionEvent {
this.stackTrace,
]);
void throwMe() {
Error.throwWithStackTrace(error, stackTrace ?? StackTrace.current);
}
final Object error;
final StackTrace? stackTrace;
}

View file

@ -242,7 +242,11 @@ class _Bloc extends Bloc<_Event, _State> implements BlocTag {
if (selectedFiles.isNotEmpty) {
final targetController = collectionsController.stream.value
.itemsControllerByCollection(ev.collection);
unawaited(targetController.addFiles(selectedFiles));
targetController.addFiles(selectedFiles).onError((e, stackTrace) {
if (e != null) {
add(_SetError(e, stackTrace));
}
});
}
}

View file

@ -14,6 +14,8 @@ import 'package:np_codegen/np_codegen.dart';
part 'add_selection_to_collection_handler.g.dart';
/// To bridge code not yet utilizing [CollectionsController] to work with those
/// that do
@npLog
class AddSelectionToCollectionHandler {
const AddSelectionToCollectionHandler();
@ -38,20 +40,11 @@ class AddSelectionToCollectionHandler {
.stream
.value
.itemsControllerByCollection(collection);
Object? error;
final s = controller.stream.listen((_) {}, onError: (e) => error = e);
await controller.addFiles(selection);
await s.cancel();
if (error != null) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global().addSelectedToAlbumFailureNotification),
duration: k.snackBarDurationNormal,
));
}
} catch (e, stackTrace) {
_log.shout("[call] Exception", e, stackTrace);
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global().addSelectedToAlbumFailureNotification),
content: Text(L10n.global().addToAlbumFailureNotification),
duration: k.snackBarDurationNormal,
));
}

View file

@ -98,13 +98,11 @@ class _WrappedHomeCollectionsState extends State<_WrappedHomeCollections>
},
),
BlocListener<_Bloc, _State>(
listenWhen: (previous, current) =>
previous.loadError != current.loadError,
listenWhen: (previous, current) => previous.error != current.error,
listener: (context, state) {
if (state.loadError != null && isPageVisible()) {
if (state.error != null && isPageVisible()) {
SnackBarManager().showSnackBar(SnackBar(
content:
Text(exception_util.toUserString(state.loadError!.error)),
content: Text(exception_util.toUserString(state.error!.error)),
duration: k.snackBarDurationNormal,
));
}

View file

@ -17,9 +17,9 @@ abstract class $_StateCopyWithWorker {
{List<Collection>? collections,
collection_util.CollectionSort? sort,
bool? isLoading,
ExceptionEvent? loadError,
List<_Item>? transformedItems,
Set<_Item>? selectedItems,
ExceptionEvent? error,
ExceptionEvent? removeError});
}
@ -31,20 +31,18 @@ class _$_StateCopyWithWorkerImpl implements $_StateCopyWithWorker {
{dynamic collections,
dynamic sort,
dynamic isLoading,
dynamic loadError = copyWithNull,
dynamic transformedItems,
dynamic selectedItems,
dynamic error = copyWithNull,
dynamic removeError = copyWithNull}) {
return _State(
collections: collections as List<Collection>? ?? that.collections,
sort: sort as collection_util.CollectionSort? ?? that.sort,
isLoading: isLoading as bool? ?? that.isLoading,
loadError: loadError == copyWithNull
? that.loadError
: loadError as ExceptionEvent?,
transformedItems:
transformedItems as List<_Item>? ?? that.transformedItems,
selectedItems: selectedItems as Set<_Item>? ?? that.selectedItems,
error: error == copyWithNull ? that.error : error as ExceptionEvent?,
removeError: removeError == copyWithNull
? that.removeError
: removeError as ExceptionEvent?);

View file

@ -38,13 +38,12 @@ class _Bloc extends Bloc<_Event, _State> implements BlocTag {
onData: (data) => state.copyWith(
collections: data.data.map((d) => d.collection).toList(),
isLoading: data.hasNext,
loadError: null,
),
onError: (e, stackTrace) {
_log.severe("[_onLoad] Uncaught exception", e, stackTrace);
return state.copyWith(
isLoading: false,
loadError: ExceptionEvent(e, stackTrace),
error: ExceptionEvent(e, stackTrace),
);
},
);

View file

@ -7,9 +7,9 @@ class _State {
required this.collections,
required this.sort,
required this.isLoading,
required this.loadError,
required this.transformedItems,
required this.selectedItems,
this.error,
required this.removeError,
});
@ -18,7 +18,6 @@ class _State {
collections: [],
sort: collection_util.CollectionSort.dateDescending,
isLoading: false,
loadError: null,
transformedItems: [],
selectedItems: {},
removeError: null,
@ -31,10 +30,10 @@ class _State {
final List<Collection> collections;
final collection_util.CollectionSort sort;
final bool isLoading;
final ExceptionEvent? loadError;
final List<_Item> transformedItems;
final Set<_Item> selectedItems;
final ExceptionEvent? error;
final ExceptionEvent? removeError;
}