feat(logging): add logging to chat bloc history and attachments

This commit is contained in:
qzl
2026-04-01 14:33:41 +08:00
parent 4fb41d2b36
commit b8e5a42a12
3 changed files with 29 additions and 2 deletions
@@ -214,7 +214,16 @@ class ChatBloc extends Cubit<ChatState> implements ChatOrchestrator {
final epoch = ++_sessionEpoch; final epoch = ++_sessionEpoch;
_activeUserId = normalizedUserId; _activeUserId = normalizedUserId;
try {
await _service.setUserContext(normalizedUserId); await _service.setUserContext(normalizedUserId);
} catch (e, stackTrace) {
_logger.error(
message: 'Failed to set user context',
error: e,
stackTrace: stackTrace,
extra: {'user_id': normalizedUserId},
);
}
if (epoch != _sessionEpoch) { if (epoch != _sessionEpoch) {
return; return;
} }
@@ -75,7 +75,13 @@ extension _ChatBlocAttachments on ChatBloc {
final bytes = await _service.fetchAttachmentPreview(previewPath); final bytes = await _service.fetchAttachmentPreview(previewPath);
_attachmentPreviewCache[previewPath] = bytes; _attachmentPreviewCache[previewPath] = bytes;
return bytes; return bytes;
} catch (_) { } catch (e, stackTrace) {
_logger.error(
message: 'Failed to load attachment preview',
error: e,
stackTrace: stackTrace,
extra: {'preview_path': previewPath},
);
return null; return null;
} finally { } finally {
_attachmentPreviewInflight.remove(previewPath); _attachmentPreviewInflight.remove(previewPath);
@@ -22,6 +22,12 @@ extension _ChatBlocHistory on ChatBloc {
hasEarlierHistory: snapshot.hasMore, hasEarlierHistory: snapshot.hasMore,
), ),
); );
} catch (e, stackTrace) {
_logger.error(
message: 'Failed to load chat history',
error: e,
stackTrace: stackTrace,
);
} finally { } finally {
if (epoch == _sessionEpoch) { if (epoch == _sessionEpoch) {
emit(state.copyWith(isLoadingHistory: false)); emit(state.copyWith(isLoadingHistory: false));
@@ -53,6 +59,12 @@ extension _ChatBlocHistory on ChatBloc {
hasEarlierHistory: snapshot.hasMore, hasEarlierHistory: snapshot.hasMore,
), ),
); );
} catch (e, stackTrace) {
_logger.error(
message: 'Failed to load more chat history',
error: e,
stackTrace: stackTrace,
);
} finally { } finally {
if (epoch == _sessionEpoch) { if (epoch == _sessionEpoch) {
emit(state.copyWith(isLoadingHistory: false)); emit(state.copyWith(isLoadingHistory: false));