Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use exceptionHandler during pigeon communication on mobile #15

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 50 additions & 0 deletions flutter_theolive_sdk_platform_interface/lib/debug_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,53 @@ void dprint(Object? object) {
print(object);
}
}

void exceptionHandler({required String tag, required Exception exception, String? context = null, StackTrace? stacktrace = null}) {
if (kDebugMode) {
throw exception;
} else {
final prefix = "[$tag]";
final ctx = context != null ? "[C:$context]" : "";
var logMessage = "THEOLogger$prefix$ctx - Exception happened: $exception";
if (!THEOLogger.instance.isListening()) {
logMessage += " - for more info, attach a logger to THEOLogger.instance!";
}
print(logMessage);
THEOLogger.instance._log(exception, stacktrace);
}
}

/**
* Logger class to capture logs and exceptions
* NOTE:
* - Experimental
* - Only used to catch PlatformExceptions for now
*/
class THEOLogger {

THEOLoggerCallback? _loggerCallback;

THEOLoggerCallback? get logger {
return _loggerCallback;
}

void set logger(THEOLoggerCallback? callback) {
_loggerCallback = callback;
}

static final THEOLogger instance = THEOLogger._constructor();
THEOLogger._constructor();

void _log(Exception e, StackTrace? stackTrace) {
_loggerCallback?.onException(e, stackTrace ?? StackTrace.current );
}

bool isListening() {
return _loggerCallback != null;
}

}

abstract class THEOLoggerCallback {
void onException(Exception e, StackTrace stackTrace);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,70 +25,84 @@ class THEOliveViewControllerMobile extends THEOliveViewController implements THE

@override
void preloadChannels(List<String> list) {
_nativeAPI.preloadChannels(list);
_nativeAPI.preloadChannels(list)
.onError<Exception>((error, stackTrace) => exceptionHandler(exception: error, tag: "PIGEON", stacktrace: stackTrace));
}

@override
void loadChannel(String channelId) {
_nativeAPI.loadChannel(channelId).onError((error, stackTrace) =>
//consume the exception, it is irrelevant to the flow, just for information
dprint("ERROR during loadChannel: $error"));
_nativeAPI.loadChannel(channelId)
.onError<Exception>((error, stackTrace) => exceptionHandler(exception: error, tag: "PIGEON", stacktrace: stackTrace));
}

@override
void play() {
_nativeAPI.play();
_nativeAPI.play()
.onError<Exception>((error, stackTrace) => exceptionHandler(exception: error, tag: "PIGEON", stacktrace: stackTrace));
}

@override
void pause() {
_nativeAPI.pause();
_nativeAPI.pause()
.onError<Exception>((error, stackTrace) => exceptionHandler(exception: error, tag: "PIGEON", stacktrace: stackTrace));
}

@override
Future<bool> isAutoplay() {
return _nativeAPI.isAutoplay();
return _nativeAPI.isAutoplay()
.onError<Exception>((error, stackTrace) {
exceptionHandler(exception: error, tag: "PIGEON", stacktrace: stackTrace);
return false;
});
}

@override
void setMuted(bool muted) {
_nativeAPI.setMuted(muted);
_nativeAPI.setMuted(muted)
.onError<Exception>((error, stackTrace) => exceptionHandler(exception: error, tag: "PIGEON", stacktrace: stackTrace));
}

@override
void setBadNetworkMode(bool badNetworkMode) {
_nativeAPI.setBadNetworkMode(badNetworkMode);
_nativeAPI.setBadNetworkMode(badNetworkMode)
.onError<Exception>((error, stackTrace) => exceptionHandler(exception: error, tag: "PIGEON", stacktrace: stackTrace));
}

@override
void goLive() {
_nativeAPI.goLive();
_nativeAPI.goLive()
.onError<Exception>((error, stackTrace) => exceptionHandler(exception: error, tag: "PIGEON", stacktrace: stackTrace));
}

@override
void reset() {
_nativeAPI.reset();
_nativeAPI.reset()
.onError<Exception>((error, stackTrace) => exceptionHandler(exception: error, tag: "PIGEON", stacktrace: stackTrace));
}

@override
void updateNativePlayerConfiguration(NativePlayerConfiguration configuration) {
final nativeConfig = PigeonNativePlayerConfiguration(sessionId: configuration.sessionId);
_nativeAPI.updateConfiguration(nativeConfig);
_nativeAPI.updateConfiguration(nativeConfig)
.onError<Exception>((error, stackTrace) => exceptionHandler(exception: error, tag: "PIGEON", stacktrace: stackTrace));
}

@override
void dispose() {
_nativeAPI.destroy();
_nativeAPI.destroy()
.onError<Exception>((error, stackTrace) => exceptionHandler(exception: error, tag: "PIGEON", stacktrace: stackTrace));
}

@override
void onLifecycleResume() {
_nativeAPI.onLifecycleResume();
_nativeAPI.onLifecycleResume()
.onError<Exception>((error, stackTrace) => exceptionHandler(exception: error, tag: "PIGEON", stacktrace: stackTrace));
}

@override
void onLifecyclePause() {
_nativeAPI.onLifecyclePause();
_nativeAPI.onLifecyclePause()
.onError<Exception>((error, stackTrace) => exceptionHandler(exception: error, tag: "PIGEON", stacktrace: stackTrace));
}

@override
Expand Down
Loading