Skip to content

Commit 72aaa17

Browse files
committed
refactor(container): Change single error handling to multiple error lists
Support the simultaneous display of multiple container operation errors, enhancing error handling capabilities
1 parent 0659e7f commit 72aaa17

File tree

5 files changed

+50
-42
lines changed

5 files changed

+50
-42
lines changed

lib/data/provider/container.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ abstract class ContainerState with _$ContainerState {
2525
@Default(null) List<ContainerPs>? items,
2626
@Default(null) List<ContainerImg>? images,
2727
@Default(null) String? version,
28-
@Default(null) ContainerErr? error,
28+
@Default(<ContainerErr>[]) List<ContainerErr> errors,
2929
@Default(null) String? runLog,
3030
@Default(ContainerType.docker) ContainerType type,
3131
@Default(false) bool isBusy,
@@ -48,7 +48,7 @@ class ContainerNotifier extends _$ContainerNotifier {
4848
}
4949

5050
Future<void> setType(ContainerType type) async {
51-
state = state.copyWith(type: type, error: null, runLog: null, items: null, images: null, version: null);
51+
state = state.copyWith(type: type, errors: [], runLog: null, items: null, images: null, version: null);
5252
Stores.container.setType(type, hostId);
5353
sudoCompleter = Completer<bool>();
5454
await refresh();
@@ -98,18 +98,20 @@ class ContainerNotifier extends _$ContainerNotifier {
9898

9999
/// Code 127 means command not found
100100
if (code == 127 || raw.contains(_dockerNotFound)) {
101-
state = state.copyWith(error: ContainerErr(type: ContainerErrType.notInstalled));
101+
state = state.copyWith(errors: [ContainerErr(type: ContainerErrType.notInstalled)]);
102102
return;
103103
}
104104

105105
// Check result segments count
106106
final segments = raw.split(ScriptConstants.separator);
107107
if (segments.length != ContainerCmdType.values.length) {
108108
state = state.copyWith(
109-
error: ContainerErr(
110-
type: ContainerErrType.segmentsNotMatch,
111-
message: 'Container segments: ${segments.length}',
112-
),
109+
errors: [
110+
ContainerErr(
111+
type: ContainerErrType.segmentsNotMatch,
112+
message: 'Container segments: ${segments.length}',
113+
),
114+
],
113115
);
114116
Loggers.app.warning('Container segments: ${segments.length}\n$raw');
115117
return;
@@ -119,10 +121,10 @@ class ContainerNotifier extends _$ContainerNotifier {
119121
final verRaw = ContainerCmdType.version.find(segments);
120122
try {
121123
final version = json.decode(verRaw)['Client']['Version'];
122-
state = state.copyWith(version: version, error: null);
124+
state = state.copyWith(version: version);
123125
} catch (e, trace) {
124126
state = state.copyWith(
125-
error: ContainerErr(type: ContainerErrType.invalidVersion, message: '$e'),
127+
errors: [...state.errors, ContainerErr(type: ContainerErrType.invalidVersion, message: '$e')],
126128
);
127129
Loggers.app.warning('Container version failed', e, trace);
128130
}
@@ -140,7 +142,7 @@ class ContainerNotifier extends _$ContainerNotifier {
140142
state = state.copyWith(items: items);
141143
} catch (e, trace) {
142144
state = state.copyWith(
143-
error: ContainerErr(type: ContainerErrType.parsePs, message: '$e'),
145+
errors: [...state.errors, ContainerErr(type: ContainerErrType.parsePs, message: '$e')],
144146
);
145147
Loggers.app.warning('Container ps failed', e, trace);
146148
}
@@ -162,7 +164,7 @@ class ContainerNotifier extends _$ContainerNotifier {
162164
state = state.copyWith(images: images);
163165
} catch (e, trace) {
164166
state = state.copyWith(
165-
error: ContainerErr(type: ContainerErrType.parseImages, message: '$e'),
167+
errors: [...state.errors, ContainerErr(type: ContainerErrType.parseImages, message: '$e')],
166168
);
167169
Loggers.app.warning('Container images failed', e, trace);
168170
}
@@ -189,7 +191,7 @@ class ContainerNotifier extends _$ContainerNotifier {
189191
}
190192
} catch (e, trace) {
191193
state = state.copyWith(
192-
error: ContainerErr(type: ContainerErrType.parseStats, message: '$e'),
194+
errors: [...state.errors, ContainerErr(type: ContainerErrType.parseStats, message: '$e')],
193195
);
194196
Loggers.app.warning('Parse docker stats: $statsRaw', e, trace);
195197
}

lib/data/provider/container.freezed.dart

Lines changed: 29 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/data/provider/container.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/data/provider/server/all.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/view/page/container/container.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ class _ContainerPageState extends ConsumerState<ContainerPage> {
5555

5656
@override
5757
Widget build(BuildContext context) {
58-
final err = ref.watch(_provider.select((p) => p.error));
58+
final errors = ref.watch(_provider.select((p) => p.errors));
5959

6060
return Scaffold(
6161
appBar: _buildAppBar(),
6262
body: SafeArea(child: _buildMain()),
63-
floatingActionButton: err == null ? _buildFAB() : null,
63+
floatingActionButton: errors.isEmpty ? _buildFAB() : null,
6464
);
6565
}
6666

@@ -84,7 +84,7 @@ class _ContainerPageState extends ConsumerState<ContainerPage> {
8484
Widget _buildMain() {
8585
final containerState = _containerState;
8686

87-
if (containerState.error != null && containerState.items == null) {
87+
if (containerState.errors.isNotEmpty && containerState.items == null) {
8888
return SizedBox.expand(
8989
child: Column(
9090
children: [
@@ -93,7 +93,7 @@ class _ContainerPageState extends ConsumerState<ContainerPage> {
9393
UIs.height13,
9494
Padding(
9595
padding: const EdgeInsets.symmetric(horizontal: 23),
96-
child: Text(containerState.error.toString()),
96+
child: Text(containerState.errors.map((e) => e.toString()).join('\n')),
9797
),
9898
const Spacer(),
9999
UIs.height13,
@@ -334,7 +334,7 @@ class _ContainerPageState extends ConsumerState<ContainerPage> {
334334
return ExpandTile(
335335
leading: const Icon(Icons.settings),
336336
title: Text(libL10n.setting),
337-
initiallyExpanded: containerState.error != null,
337+
initiallyExpanded: containerState.errors.isNotEmpty,
338338
children: _SettingsMenuItems.values.map((item) => _buildSettingTile(item, containerState)).toList(),
339339
).cardx;
340340
}

0 commit comments

Comments
 (0)