From b8e5a42a12ae38f38ab4c57f11e86f23e1450176 Mon Sep 17 00:00:00 2001 From: qzl Date: Wed, 1 Apr 2026 14:33:41 +0800 Subject: [PATCH] feat(logging): add logging to chat bloc history and attachments --- .../features/chat/presentation/bloc/chat_bloc.dart | 11 ++++++++++- .../presentation/bloc/chat_bloc_attachments.dart | 8 +++++++- .../chat/presentation/bloc/chat_bloc_history.dart | 12 ++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/apps/lib/features/chat/presentation/bloc/chat_bloc.dart b/apps/lib/features/chat/presentation/bloc/chat_bloc.dart index 46829bd..59564aa 100644 --- a/apps/lib/features/chat/presentation/bloc/chat_bloc.dart +++ b/apps/lib/features/chat/presentation/bloc/chat_bloc.dart @@ -214,7 +214,16 @@ class ChatBloc extends Cubit implements ChatOrchestrator { final epoch = ++_sessionEpoch; _activeUserId = normalizedUserId; - await _service.setUserContext(normalizedUserId); + try { + 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) { return; } diff --git a/apps/lib/features/chat/presentation/bloc/chat_bloc_attachments.dart b/apps/lib/features/chat/presentation/bloc/chat_bloc_attachments.dart index 59fee76..2976a73 100644 --- a/apps/lib/features/chat/presentation/bloc/chat_bloc_attachments.dart +++ b/apps/lib/features/chat/presentation/bloc/chat_bloc_attachments.dart @@ -75,7 +75,13 @@ extension _ChatBlocAttachments on ChatBloc { final bytes = await _service.fetchAttachmentPreview(previewPath); _attachmentPreviewCache[previewPath] = 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; } finally { _attachmentPreviewInflight.remove(previewPath); diff --git a/apps/lib/features/chat/presentation/bloc/chat_bloc_history.dart b/apps/lib/features/chat/presentation/bloc/chat_bloc_history.dart index 2d500f4..a377a6d 100644 --- a/apps/lib/features/chat/presentation/bloc/chat_bloc_history.dart +++ b/apps/lib/features/chat/presentation/bloc/chat_bloc_history.dart @@ -22,6 +22,12 @@ extension _ChatBlocHistory on ChatBloc { hasEarlierHistory: snapshot.hasMore, ), ); + } catch (e, stackTrace) { + _logger.error( + message: 'Failed to load chat history', + error: e, + stackTrace: stackTrace, + ); } finally { if (epoch == _sessionEpoch) { emit(state.copyWith(isLoadingHistory: false)); @@ -53,6 +59,12 @@ extension _ChatBlocHistory on ChatBloc { hasEarlierHistory: snapshot.hasMore, ), ); + } catch (e, stackTrace) { + _logger.error( + message: 'Failed to load more chat history', + error: e, + stackTrace: stackTrace, + ); } finally { if (epoch == _sessionEpoch) { emit(state.copyWith(isLoadingHistory: false));