Created
February 8, 2023 18:58
-
-
Save ueman/e002523741b496270dcabef2683e136e to your computer and use it in GitHub Desktop.
Sentry Bloc Observer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:developer'; | |
import 'package:bloc/bloc.dart'; | |
import 'package:sentry/sentry.dart'; | |
class SentryBlocObserver extends BlocObserver { | |
SentryBlocObserver({Hub? hub}) : _hub = hub ?? HubAdapter(); | |
final Hub _hub; | |
@override | |
void onEvent(Bloc<dynamic, dynamic> bloc, Object? event) { | |
super.onEvent(bloc, event); | |
_hub.addBreadcrumb( | |
Breadcrumb( | |
type: 'bloc', | |
category: 'onEvent', | |
message: event?.toString(), | |
data: { | |
'bloc': bloc.runtimeType, | |
}, | |
), | |
); | |
} | |
@override | |
void onChange(BlocBase<dynamic> bloc, Change<dynamic> change) { | |
super.onChange(bloc, change); | |
_hub.addBreadcrumb( | |
Breadcrumb( | |
type: 'bloc', | |
category: 'onChange', | |
message: change.toString(), | |
data: { | |
'bloc': bloc.runtimeType, | |
}, | |
), | |
); | |
} | |
@override | |
void onTransition( | |
Bloc<dynamic, dynamic> bloc, | |
Transition<dynamic, dynamic> transition, | |
) { | |
super.onTransition(bloc, transition); | |
_hub.addBreadcrumb( | |
Breadcrumb( | |
type: 'bloc', | |
category: 'onTransition', | |
message: transition.toString(), | |
data: { | |
'bloc': bloc.runtimeType, | |
}, | |
), | |
); | |
} | |
@override | |
void onClose(BlocBase<dynamic> bloc) { | |
super.onClose(bloc); | |
_hub.addBreadcrumb( | |
Breadcrumb( | |
type: 'bloc', | |
category: 'onClose', | |
data: { | |
'bloc': bloc.runtimeType, | |
}, | |
), | |
); | |
} | |
@override | |
void onError(BlocBase<dynamic> bloc, Object error, StackTrace stackTrace) { | |
super.onError(bloc, error, stackTrace); | |
final mechanism = Mechanism( | |
type: 'BlocObserver.onError', | |
handled: false, | |
); | |
final throwableMechanism = ThrowableMechanism(mechanism, error); | |
final event = SentryEvent( | |
throwable: throwableMechanism, | |
level: SentryLevel.fatal, | |
); | |
_hub.captureEvent(event, stackTrace: stackTrace); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment