Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions packages/capsicum/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import 'src/service/desktop_notification_dispatcher.dart';
import 'src/service/launch_at_login_service.dart';
import 'src/service/resident_mode_service.dart';
import 'src/service/apns_service.dart';
import 'src/util/exception_scrub.dart';
import 'src/service/fcm_service.dart';
import 'src/service/notification_init.dart';
import 'src/service/notification_label_cache.dart';
Expand All @@ -43,6 +42,7 @@ import 'src/service/share_intent_service.dart';
import 'src/service/timeline_cache.dart';
import 'src/service/window_state_service.dart';
import 'src/service/wns_service.dart';
import 'src/util/exception_scrub.dart';
import 'src/util/sentry_observability.dart';
import 'src/util/sentry_tag_hash.dart';
import 'src/util/shared_preferences_cache.dart';
Expand Down Expand Up @@ -94,6 +94,22 @@ void _logDev(String message) {
if (!kReleaseMode) debugPrint(message);
}

/// [_logDev] の例外版。**捕捉した例外は必ずこちらへ流すこと** (#975)。
///
/// `_logDev` は release では no-op だが **profile では debugPrint に流れる**。
/// sentry_flutter の `DebugPrintIntegration` は build mode で分岐せず登録される
/// ので、DSN 付きビルドでは breadcrumb になる。breadcrumb の `message` は
/// [_scrubBreadcrumb](`data` しか見ない)を通らないため、生の例外を埋めると
/// DioException の URL がそのまま Sentry に載る。
void _logDevException(String context, Object error, [StackTrace? stackTrace]) {
final scrubbed = scrubException(error);
_logDev(
stackTrace == null
? '$context: $scrubbed'
: '$context: $scrubbed\n$stackTrace',
);
}

/// [_logDev] の StackTrace 版。
void _logDevStack(StackTrace stackTrace) {
if (!kReleaseMode) debugPrintStack(stackTrace: stackTrace);
Expand Down Expand Up @@ -134,7 +150,7 @@ Future<void> main() async {
try {
await PushKeyStore.migrateAccessibilityIfNeeded();
} catch (e, st) {
_logDev('PushKeyStore migration failed: $e\n$st');
_logDevException('PushKeyStore migration failed', e, st);
}

// アカウント secret / client credentials も v1.30 以前は旧 Keychain
Expand All @@ -146,7 +162,7 @@ Future<void> main() async {
try {
await AccountStorage().migrateAccessibilityIfNeeded();
} catch (e, st) {
_logDev('AccountStorage migration failed: $e\n$st');
_logDevException('AccountStorage migration failed', e, st);
}

// 起動キャッシュ (#890) の保存先を Application Support からキャッシュ領域へ
Expand Down Expand Up @@ -199,7 +215,7 @@ Future<void> main() async {
try {
sentryNativeDbPath = await resolveSentryNativeDatabasePath();
} catch (e, st) {
_logDev('resolveSentryNativeDatabasePath failed: $e\n$st');
_logDevException('resolveSentryNativeDatabasePath failed', e, st);
}
await SentryFlutter.init((options) {
options.dsn = dsn;
Expand Down Expand Up @@ -455,7 +471,7 @@ void _startApp() {
// Sentry が自前で設定済なので ??= で上書きを避ける。
FlutterError.onError ??= FlutterError.presentError;
PlatformDispatcher.instance.onError ??= (e, st) {
_logDev('Uncaught: $e\n$st');
_logDevException('Uncaught', e, st);
return true;
};

Expand Down Expand Up @@ -822,7 +838,7 @@ void _routeToChatThread(
} catch (e, st) {
// user 解決失敗 (削除済み・通信エラー等) は通知タブにフォールバック。
Sentry.captureException(
e,
scrubException(e),
stackTrace: st,
withScope: (scope) {
scope.setTag('notification.routing', 'chat.user_resolve_failed');
Expand Down Expand Up @@ -1156,9 +1172,9 @@ Future<void> _initFirebase() async {
});
_logDev('capsicum: push.onMessage listener registered');
} catch (e, st) {
_logDev('capsicum: Firebase initialization failed: $e');
_logDevException('capsicum: Firebase initialization failed', e);
Sentry.captureException(
e,
scrubException(e),
stackTrace: st,
withScope: (scope) {
scope.setTag('service', 'firebase_init');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:sentry_flutter/sentry_flutter.dart';

import '../../util/exception_scrub.dart';
import 'background_task_scheduler.dart';

/// macOS / Linux / Windows 向け実装。Dart の [Timer.periodic] を使った
Expand Down Expand Up @@ -50,12 +51,14 @@ class TimerBackgroundTaskScheduler
} catch (e, st) {
// スケジューラは次回 interval まで待たせ、ここで例外を握って
// タスクを止めない。観測層で taskId 単位にグループしておく。
debugPrint(
'capsicum: background_task: callback failed (taskId=$taskId): $e\n$st',
debugLogException(
'capsicum: background_task: callback failed (taskId=$taskId)',
e,
st,
);
try {
await Sentry.captureException(
e,
scrubException(e),
stackTrace: st,
withScope: (scope) {
scope.setTag('background_task.id', taskId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class FlutterLocalNotificationSubsystem implements NotificationSubsystem {
// 失敗が UI を巻き込まないよう更に try で包む。
try {
await Sentry.captureException(
e,
scrubException(e),
stackTrace: st,
hint: Hint.withMap({'subsystem': 'notification'}),
);
Expand Down
19 changes: 11 additions & 8 deletions packages/capsicum/lib/src/provider/account_manager_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import '../service/push_registration_service.dart';
import '../service/server_metadata_cache.dart';
import '../service/timeline_cache.dart';
import '../service/wns_service.dart';
import '../util/exception_scrub.dart';
import '../util/login_error.dart';
import '../util/sentry_tag_hash.dart';
import '../util/exception_scrub.dart';

/// State: list of accounts + currently selected account.
class AccountManagerState {
Expand Down Expand Up @@ -150,9 +150,10 @@ class AccountManagerNotifier extends Notifier<AccountManagerState> {
try {
await adapter.detectTimelineAvailability();
} catch (e) {
debugPrint(
debugLogException(
'capsicum: addAccount: detectTimelineAvailability failed for '
'${account.key.toStorageKey()}: $e',
'${account.key.toStorageKey()}',
e,
);
}
}
Expand Down Expand Up @@ -729,9 +730,10 @@ class AccountManagerNotifier extends Notifier<AccountManagerState> {
// させる。auth 失効(401/403)や secret 系・不明は従来通り skip + 観測。
final outcome = classifyRestoreFailure(e);
if (outcome == RestoreOutcome.retriable) {
debugPrint(
debugLogException(
'capsicum: restoreSessions: transient failure for $keyStr, '
'kept offline and will retry in background: $e',
'kept offline and will retry in background',
e,
);
return (account: null, outcome: RestoreOutcome.retriable);
}
Expand Down Expand Up @@ -780,9 +782,10 @@ class AccountManagerNotifier extends Notifier<AccountManagerState> {
final userFuture = adapter.getMyself();
final timelineFuture = adapter is MastodonAdapter
? adapter.detectTimelineAvailability().catchError((Object e) {
debugPrint(
debugLogException(
'capsicum: restoreSessions: detectTimelineAvailability '
'failed for $keyStr: $e',
'failed for $keyStr',
e,
);
})
: Future<void>.value();
Expand Down Expand Up @@ -1160,7 +1163,7 @@ class AccountManagerNotifier extends Notifier<AccountManagerState> {
parsed = null;
}
Sentry.captureException(
e,
scrubException(e),
stackTrace: st,
withScope: (scope) {
// null 経路 (`_reportNullSecretSkipOnce`) と区別するための経路タグ。
Expand Down
5 changes: 3 additions & 2 deletions packages/capsicum/lib/src/provider/drive_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:capsicum_core/capsicum_core.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:sentry_flutter/sentry_flutter.dart';

import '../util/exception_scrub.dart';
import 'account_manager_provider.dart';
import 'timeline_provider.dart' show loadMoreMaxRetries, loadMoreRetryDelay;

Expand Down Expand Up @@ -95,7 +96,7 @@ class DriveContentsNotifier
} catch (e, st) {
try {
Sentry.captureException(
e,
scrubException(e),
stackTrace: st,
hint: Hint.withMap({'folderId': arg ?? 'root'}),
);
Expand Down Expand Up @@ -240,7 +241,7 @@ class DriveContentsNotifier
try {
final failedMaxId = current.files.lastOrNull?.id;
Sentry.captureException(
e,
scrubException(e),
stackTrace: st,
hint: Hint.withMap({
'folderId': arg ?? 'root',
Expand Down
2 changes: 1 addition & 1 deletion packages/capsicum/lib/src/provider/instance_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final currentInstanceProvider = FutureProvider<Instance?>((ref) async {
return await adapter.getInstance();
} catch (e, st) {
debugLogException('getInstance failed', e);
Sentry.captureException(e, stackTrace: st);
Sentry.captureException(scrubException(e), stackTrace: st);
return null;
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ final customEmojisProvider = FutureProvider<List<CustomEmoji>>((ref) async {
// 化してしまう (#609 false positive)。AsyncError として伝播させて
// consumer の `valueOrNull == null` 判定で警告抑制経路に揃える。
debugLogException('getEmojis failed', e);
Sentry.captureException(e, stackTrace: st);
Sentry.captureException(scrubException(e), stackTrace: st);
rethrow;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/capsicum/lib/src/provider/timeline_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1824,7 +1824,7 @@ class TimelineNotifier extends AutoDisposeAsyncNotifier<TimelineState> {
try {
final failedMaxId = current.posts.lastOrNull?.id;
Sentry.captureException(
e,
scrubException(e),
stackTrace: st,
hint: Hint.withMap({
'maxId': failedMaxId ?? 'null',
Expand Down
32 changes: 19 additions & 13 deletions packages/capsicum/lib/src/service/account_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ class AccountStorage {
// plugin register race が retry 後も解消しないケース。同じ race で
// delete も失敗するため、ここでは観測のみ行い secret は残す。
// 次回起動で再試行される。
debugPrint(
'capsicum: plugin register race persisted for $accountKey: $e',
debugLogException(
'capsicum: plugin register race persisted for $accountKey',
e,
);
_reportOnce('secret:$accountKey', e, st);
// secret は消していないので transient。ログアウト扱いにしない (#959)。
Expand All @@ -129,8 +130,9 @@ class AccountStorage {
// 画面ロック解除前にアプリが起動した場合などで観測される
// (CAPSICUM-1M, #531)。
if (_isKeychainTransient(e)) {
debugPrint(
'capsicum: keychain transient for $accountKey (code=${e.code}): $e',
debugLogException(
'capsicum: keychain transient for $accountKey (code=${e.code})',
e,
);
_reportOnce('secret:$accountKey:transient', e, st, code: e.code);
// secret は残っている。解錠後の再試行で読めるので transient (#959)。
Expand All @@ -144,9 +146,10 @@ class AccountStorage {
// が secret を上書きするので無害)。-25308 のような明示 transient コードが
// 無い Android では _isKeychainTransient が拾えないため、ここで分岐する。
if (Platform.isAndroid) {
debugPrint(
debugLogException(
'capsicum: android keystore read error for $accountKey, '
'keeping secret (code=${e.code}): $e',
'keeping secret (code=${e.code})',
e,
);
_reportOnce('secret:$accountKey:android_keystore', e, st, code: e.code);
// secret を消していないので transient 扱い(次回起動で再試行)(#959)。
Expand All @@ -160,8 +163,9 @@ class AccountStorage {
// BadPaddingException etc. may bypass PlatformException wrapping
// after app reinstall (encryption key regenerated). Android では上と同様、
// 一過性の Keystore 失敗で破壊しないよう delete を見送る (#730 / #731)。
debugPrint(
'capsicum: unexpected error reading secrets for $accountKey: $e',
debugLogException(
'capsicum: unexpected error reading secrets for $accountKey',
e,
);
if (Platform.isAndroid) {
_reportOnce('secret:$accountKey:android_keystore', e, st);
Expand Down Expand Up @@ -321,8 +325,9 @@ class AccountStorage {
} on PlatformException catch (e, st) {
// ロック中 (-25308) 等で readAll / write が失敗しても起動は止めない。
// flag を立てないので次回起動で再試行される。
debugPrint(
'capsicum: account storage accessibility migration failed: $e',
debugLogException(
'capsicum: account storage accessibility migration failed',
e,
);
_reportOnce('accessibility_migration', e, st);
}
Expand Down Expand Up @@ -422,8 +427,9 @@ class AccountStorage {
try {
await _storage.delete(key: key);
} on MissingPluginException catch (e, st) {
debugPrint(
'capsicum: plugin register race on delete for $accountKey: $e',
debugLogException(
'capsicum: plugin register race on delete for $accountKey',
e,
);
_reportOnce('secret:$accountKey:delete', e, st);
} on PlatformException catch (e, st) {
Expand Down Expand Up @@ -582,7 +588,7 @@ class AccountStorage {
if (!_reportedErrors.add(key)) return;
try {
Sentry.captureException(
error,
scrubException(error),
stackTrace: st,
withScope: code != null
? (scope) => scope.setTag('secret.code', code)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ class AnnouncementSubscriptionService {
await enable(account);
} catch (e) {
// enable 内で Sentry 報告済み。本筋は止めない。
debugPrint(
debugLogException(
'capsicum: announcement_subscription: auto-enable skipped for '
'${account.key.host}: $e',
'${account.key.host}',
e,
);
}
}
Expand Down Expand Up @@ -229,8 +230,9 @@ class AnnouncementSubscriptionService {
String host, {
required String phase,
}) {
debugPrint(
'capsicum: announcement_subscription: $phase failed for $host: $e',
debugLogException(
'capsicum: announcement_subscription: $phase failed for $host',
e,
);
Sentry.captureException(
scrubException(e),
Expand Down
2 changes: 1 addition & 1 deletion packages/capsicum/lib/src/service/fcm_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class FcmService {
} catch (e, st) {
debugLogException('capsicum: push.fcm: initialization failed', e);
Sentry.captureException(
e,
scrubException(e),
stackTrace: st,
withScope: (scope) {
scope.setTag('service', 'fcm_init');
Expand Down
16 changes: 10 additions & 6 deletions packages/capsicum/lib/src/service/push_registration_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,10 @@ class PushRegistrationService {
// 本筋は止めない (relay 障害で push 全体を失敗にする筋ではない)。
await AnnouncementSubscriptionService.autoEnableIfDefault(account);
} catch (e, st) {
debugPrint(
'capsicum: push.registration: failed for ${account.key.host}: $e\n$st',
debugLogException(
'capsicum: push.registration: failed for ${account.key.host}',
e,
st,
);
// NB: 登録フェーズの失敗では relay row を触らない。
//
Expand Down Expand Up @@ -411,8 +413,9 @@ class PushRegistrationService {
);
}
} catch (e, st) {
debugPrint(
'capsicum: push.registration: adapter unsubscribe failed: $e',
debugLogException(
'capsicum: push.registration: adapter unsubscribe failed',
e,
);
_reportUnregisterFailure(e, st, account.key.host, 'adapter');
}
Expand Down Expand Up @@ -465,9 +468,10 @@ class PushRegistrationService {
final id = await PushKeyStore.getRelayId(a.key.toStorageKey());
if (id != null) ids.add(id);
} catch (e) {
debugPrint(
debugLogException(
'capsicum: push.registration: relay id read failed for '
'${a.key.toStorageKey()}: $e',
'${a.key.toStorageKey()}',
e,
);
}
}
Expand Down
Loading