Skip to content

Commit

Permalink
Merge pull request #2336 from acterglobal/ben-better-logging
Browse files Browse the repository at this point in the history
Two more cases of Sentry logging
  • Loading branch information
gnunicorn authored Oct 30, 2024
2 parents ac24e25 + c1b9460 commit 3881143
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 36 deletions.
2 changes: 2 additions & 0 deletions app/lib/common/providers/notifiers/sync_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:acter/common/extensions/options.dart';
import 'package:acter/common/models/sync_state/sync_state.dart';
import 'package:acter_flutter_sdk/acter_flutter_sdk_ffi.dart' as ffi;
import 'package:riverpod/riverpod.dart';
import 'package:sentry_flutter/sentry_flutter.dart';

// ignore_for_file: avoid_print
class SyncNotifier extends StateNotifier<SyncState> {
Expand Down Expand Up @@ -64,6 +65,7 @@ class SyncNotifier extends StateNotifier<SyncState> {

_errorListener = syncState.syncErrorRx(); // keep it resident in memory
_errorPoller = _errorListener.listen((msg) {
Sentry.captureMessage('Sync failure: $msg', level: SentryLevel.error);
if (mounted) {
if (msg == 'SoftLogout' || msg == 'Unauthorized') {
// regular logout, we do nothing here
Expand Down
44 changes: 44 additions & 0 deletions app/lib/features/auth/actions/register_action.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:acter/features/auth/providers/auth_providers.dart';
import 'package:acter/features/super_invites/providers/super_invites_providers.dart';
import 'package:acter_flutter_sdk/acter_flutter_sdk_ffi.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:sentry_flutter/sentry_flutter.dart';

final _log = Logger('a3::auth::register');

Future<void> _tryRedeem(SuperInvites superInvites, String token) async {
// try to redeem the token in a fire-and-forget-manner
try {
await superInvites.redeem(token);
} catch (error, stack) {
Sentry.captureMessage('Redeeming post-registration token $token failed');
Sentry.captureException(error, stackTrace: stack);
_log.warning('redeeming super invite `$token` failed: $error');
}
}

Future<bool> register({
required String username,
required String password,
required String name,
required String token,
required WidgetRef ref,
}) async {
final authNotifier = ref.read(authStateProvider.notifier);
final errorMsg = await authNotifier.register(
username,
password,
name,
token,
);
if (errorMsg != null) {
_log.severe('Failed to register', errorMsg);
throw errorMsg;
}
if (token.isNotEmpty) {
final superInvites = ref.read(superInvitesProvider);
_tryRedeem(superInvites, token);
}
return true;
}
53 changes: 19 additions & 34 deletions app/lib/features/auth/pages/register_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import 'package:acter/common/toolkit/buttons/primary_action_button.dart';
import 'package:acter/common/utils/constants.dart';
import 'package:acter/common/utils/routes.dart';
import 'package:acter/common/widgets/no_internet.dart';
import 'package:acter/features/auth/actions/register_action.dart';
import 'package:acter/features/auth/providers/auth_providers.dart';
import 'package:acter/features/super_invites/providers/super_invites_providers.dart';
import 'package:acter_flutter_sdk/acter_flutter_sdk_ffi.dart';
import 'package:atlas_icons/atlas_icons.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
Expand All @@ -20,15 +19,6 @@ import 'package:logging/logging.dart';

final _log = Logger('a3::auth::register');

Future<void> tryRedeem(SuperInvites superInvites, String token) async {
// try to redeem the token in a fire-and-forget-manner
try {
await superInvites.redeem(token);
} catch (error) {
_log.warning('redeeming super invite failed: $error');
}
}

class RegisterPage extends ConsumerStatefulWidget {
static const usernameField = Key('reg-username-txt');
static const passwordField = Key('reg-password-txt');
Expand Down Expand Up @@ -59,32 +49,27 @@ class _RegisterPageState extends ConsumerState<RegisterPage> {
showNoInternetNotification(context);
return;
}
final authNotifier = ref.read(authStateProvider.notifier);
final errorMsg = await authNotifier.register(
username.text,
password.text,
name.text,
token.text,
context,
);
if (errorMsg != null) {
_log.severe('Failed to register', errorMsg);
if (!context.mounted) return;
final lang = L10n.of(context);
try {
if (await register(
username: username.text,
password: password.text,
name: name.text,
token: token.text,
ref: ref,
)) {
if (context.mounted) {
context.goNamed(
Routes.saveUsername.name,
queryParameters: {'username': username.text},
);
}
}
} catch (errorMsg) {
EasyLoading.showError(
L10n.of(context).registerFailed(errorMsg),
lang.registerFailed(errorMsg),
duration: const Duration(seconds: 3),
);
return;
}
if (token.text.isNotEmpty) {
final superInvites = ref.read(superInvitesProvider);
tryRedeem(superInvites, token.text);
}
if (context.mounted) {
context.goNamed(
Routes.saveUsername.name,
queryParameters: {'username': username.text},
);
}
}

Expand Down
3 changes: 1 addition & 2 deletions app/lib/features/auth/providers/notifiers/auth_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class AuthStateNotifier extends StateNotifier<bool> {
String password,
String displayName,
String token,
BuildContext context,
) async {
state = true;
final sdk = await ref.read(sdkProvider.future);
Expand All @@ -64,7 +63,7 @@ class AuthStateNotifier extends StateNotifier<bool> {
return null;
} catch (e) {
state = false;
return e.toString();
rethrow;
}
}

Expand Down

0 comments on commit 3881143

Please sign in to comment.