add sentry
This commit is contained in:
@@ -89,13 +89,8 @@ class FrappeAuthService {
|
||||
|
||||
const url = '${ApiConstants.baseUrl}${ApiConstants.frappeApiMethod}${ApiConstants.frappeLogin}';
|
||||
|
||||
// Build cookie header
|
||||
// Get stored session - only need sid and csrf_token
|
||||
final storedSession = await getStoredSession();
|
||||
final cookieHeader = _buildCookieHeader(
|
||||
sid: storedSession!['sid']!,
|
||||
fullName: storedSession['fullName']!,
|
||||
userId: storedSession['userId']!,
|
||||
);
|
||||
|
||||
final response = await _dio.post<Map<String, dynamic>>(
|
||||
url,
|
||||
@@ -109,7 +104,7 @@ class FrappeAuthService {
|
||||
},
|
||||
options: Options(
|
||||
headers: {
|
||||
'Cookie': cookieHeader,
|
||||
'Cookie': 'sid=${storedSession!['sid']!}',
|
||||
'X-Frappe-Csrf-Token': storedSession['csrfToken']!,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
@@ -203,31 +198,13 @@ class FrappeAuthService {
|
||||
return sid != null && csrfToken != null;
|
||||
}
|
||||
|
||||
/// Build cookie header string
|
||||
String _buildCookieHeader({
|
||||
required String sid,
|
||||
required String fullName,
|
||||
required String userId,
|
||||
}) {
|
||||
return [
|
||||
'sid=$sid',
|
||||
'full_name=$fullName',
|
||||
'system_user=no',
|
||||
'user_id=${Uri.encodeComponent(userId)}',
|
||||
'user_image=',
|
||||
].join('; ');
|
||||
}
|
||||
|
||||
/// Get headers for Frappe API requests
|
||||
/// Only sends sid in Cookie - other fields are not needed
|
||||
Future<Map<String, String>> getHeaders() async {
|
||||
final session = await ensureSession();
|
||||
|
||||
return {
|
||||
'Cookie': _buildCookieHeader(
|
||||
sid: session['sid']!,
|
||||
fullName: session['fullName']!,
|
||||
userId: session['userId']!,
|
||||
),
|
||||
'Cookie': 'sid=${session['sid']!}',
|
||||
'X-Frappe-Csrf-Token': session['csrfToken']!,
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
@@ -4,12 +4,16 @@ import 'package:onesignal_flutter/onesignal_flutter.dart';
|
||||
/// OneSignal service for managing push notifications and external user ID.
|
||||
///
|
||||
/// This service handles:
|
||||
/// - Initializing OneSignal SDK
|
||||
/// - Setting external user ID after login (using phone number)
|
||||
/// - Restoring external user ID on app startup
|
||||
/// - Clearing external user ID on logout
|
||||
///
|
||||
/// Usage:
|
||||
/// ```dart
|
||||
/// // Initialize in main.dart
|
||||
/// await OneSignalService.init(appId: 'your-app-id');
|
||||
///
|
||||
/// // After successful login
|
||||
/// await OneSignalService.login(phoneNumber);
|
||||
///
|
||||
@@ -19,6 +23,78 @@ import 'package:onesignal_flutter/onesignal_flutter.dart';
|
||||
class OneSignalService {
|
||||
OneSignalService._();
|
||||
|
||||
/// OneSignal App ID - Replace with your actual App ID from OneSignal dashboard
|
||||
static const String _defaultAppId = '778ca22d-c719-4ec8-86cb-a6b911166066';
|
||||
|
||||
/// Initialize OneSignal SDK
|
||||
///
|
||||
/// Must be called before using any other OneSignal methods.
|
||||
/// Sets up push subscription observers and requests notification permission.
|
||||
///
|
||||
/// [appId] - Optional App ID override (uses default if not provided)
|
||||
/// [requestPermission] - Whether to request notification permission (default: true)
|
||||
static Future<void> init({
|
||||
String? appId,
|
||||
bool requestPermission = true,
|
||||
}) async {
|
||||
try {
|
||||
// Set debug log level (verbose in debug, none in release)
|
||||
OneSignal.Debug.setLogLevel(kDebugMode ? OSLogLevel.verbose : OSLogLevel.none);
|
||||
|
||||
// Initialize with App ID
|
||||
OneSignal.initialize(appId ?? _defaultAppId);
|
||||
debugPrint('🔔 OneSignal initialized');
|
||||
|
||||
// Add push subscription observer to track subscription state changes
|
||||
OneSignal.User.pushSubscription.addObserver((state) {
|
||||
debugPrint('🔔 Push subscription state changed:');
|
||||
debugPrint(' Previous - optedIn: ${state.previous.optedIn}, id: ${state.previous.id}');
|
||||
debugPrint(' Current - optedIn: ${state.current.optedIn}, id: ${state.current.id}');
|
||||
debugPrint(' Subscription ID: ${state.current.id}');
|
||||
debugPrint(' Push Token: ${state.current.token}');
|
||||
|
||||
if (state.current.id != null) {
|
||||
debugPrint('🔔 ✅ Device successfully subscribed!');
|
||||
}
|
||||
});
|
||||
|
||||
// Add notification permission observer
|
||||
OneSignal.Notifications.addPermissionObserver((isGranted) {
|
||||
debugPrint('🔔 Notification permission changed: $isGranted');
|
||||
});
|
||||
|
||||
// Request permission if enabled
|
||||
if (requestPermission) {
|
||||
final accepted = await OneSignal.Notifications.requestPermission(true);
|
||||
debugPrint('🔔 Permission request result: $accepted');
|
||||
}
|
||||
|
||||
// Give OneSignal SDK time to complete initialization and server registration
|
||||
await Future<void>.delayed(const Duration(seconds: 2));
|
||||
|
||||
// Log current subscription status
|
||||
_logSubscriptionStatus();
|
||||
} catch (e) {
|
||||
debugPrint('🔔 OneSignal error: Failed to initialize - $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Log current subscription status for debugging
|
||||
static void _logSubscriptionStatus() {
|
||||
final optedIn = OneSignal.User.pushSubscription.optedIn;
|
||||
final id = OneSignal.User.pushSubscription.id;
|
||||
final token = OneSignal.User.pushSubscription.token;
|
||||
|
||||
debugPrint('🔔 Current subscription status:');
|
||||
debugPrint(' Opted In: $optedIn');
|
||||
debugPrint(' Subscription ID: $id');
|
||||
debugPrint(' Push Token: $token');
|
||||
|
||||
if (id == null) {
|
||||
debugPrint('🔔 ⚠️ Subscription ID is null - check device connectivity and OneSignal app ID');
|
||||
}
|
||||
}
|
||||
|
||||
/// Login user to OneSignal by setting external user ID.
|
||||
///
|
||||
/// This associates the device with the user's phone number,
|
||||
|
||||
250
lib/core/services/sentry_service.dart
Normal file
250
lib/core/services/sentry_service.dart
Normal file
@@ -0,0 +1,250 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:sentry_flutter/sentry_flutter.dart';
|
||||
|
||||
/// Sentry service for error tracking and performance monitoring.
|
||||
///
|
||||
/// This service handles:
|
||||
/// - Initializing Sentry SDK
|
||||
/// - Capturing exceptions and errors
|
||||
/// - Capturing custom messages
|
||||
/// - Setting user context after login
|
||||
/// - Performance monitoring
|
||||
///
|
||||
/// Usage:
|
||||
/// ```dart
|
||||
/// // Initialize in main.dart
|
||||
/// await SentryService.init(
|
||||
/// dsn: 'your-sentry-dsn',
|
||||
/// appRunner: () => runApp(MyApp()),
|
||||
/// );
|
||||
///
|
||||
/// // Capture exception
|
||||
/// SentryService.captureException(error, stackTrace: stackTrace);
|
||||
///
|
||||
/// // Capture message
|
||||
/// SentryService.captureMessage('User performed action X');
|
||||
///
|
||||
/// // Set user context after login
|
||||
/// SentryService.setUser(userId: '123', email: 'user@example.com');
|
||||
/// ```
|
||||
class SentryService {
|
||||
SentryService._();
|
||||
|
||||
/// Sentry DSN - Replace with your actual DSN from Sentry dashboard
|
||||
static const String _dsn = 'https://2c5893508a29e5ea750b64d5ee31aeef@o4509632266436608.ingest.us.sentry.io/4510464530972672';
|
||||
|
||||
/// Initialize Sentry SDK
|
||||
///
|
||||
/// Must be called before runApp() in main.dart.
|
||||
/// Wraps the app with Sentry error boundary.
|
||||
///
|
||||
/// [dsn] - Optional DSN override (uses default if not provided)
|
||||
/// [appRunner] - The function that runs the app (typically runApp(MyApp()))
|
||||
/// [environment] - Environment name (e.g., 'development', 'production')
|
||||
static Future<void> init({
|
||||
String? dsn,
|
||||
required Future<void> Function() appRunner,
|
||||
String? environment,
|
||||
}) async {
|
||||
// Get package info for release version
|
||||
final packageInfo = await PackageInfo.fromPlatform();
|
||||
final release = 'partner@${packageInfo.version}+${packageInfo.buildNumber}';
|
||||
|
||||
await SentryFlutter.init(
|
||||
(options) {
|
||||
options
|
||||
..dsn = dsn ?? _dsn
|
||||
|
||||
// Release version: worker@1.0.1+29
|
||||
..release = release
|
||||
|
||||
// Environment configuration
|
||||
..environment = environment ?? (kReleaseMode ? 'production' : 'development')
|
||||
|
||||
// Performance monitoring
|
||||
..tracesSampleRate = kReleaseMode ? 0.2 : 1.0 // 20% in prod, 100% in dev
|
||||
..profilesSampleRate = kReleaseMode ? 0.2 : 1.0
|
||||
|
||||
// Enable automatic instrumentation
|
||||
..enableAutoPerformanceTracing = true
|
||||
|
||||
// Capture failed requests
|
||||
..captureFailedRequests = true
|
||||
|
||||
// Debug mode settings
|
||||
..debug = kDebugMode
|
||||
|
||||
// Add app-specific tags
|
||||
..beforeSend = (event, hint) {
|
||||
// Filter out certain errors if needed
|
||||
// Return null to drop the event
|
||||
return event;
|
||||
};
|
||||
},
|
||||
appRunner: appRunner,
|
||||
);
|
||||
|
||||
debugPrint('🔴 Sentry initialized (release: $release, enabled: ${!kDebugMode})');
|
||||
}
|
||||
|
||||
/// Capture an exception with optional stack trace
|
||||
///
|
||||
/// [exception] - The exception to capture
|
||||
/// [stackTrace] - Optional stack trace
|
||||
/// [hint] - Optional hint with additional context
|
||||
static Future<void> captureException(
|
||||
dynamic exception, {
|
||||
StackTrace? stackTrace,
|
||||
Hint? hint,
|
||||
}) async {
|
||||
try {
|
||||
await Sentry.captureException(
|
||||
exception,
|
||||
stackTrace: stackTrace,
|
||||
hint: hint,
|
||||
);
|
||||
debugPrint('🔴 Sentry: Exception captured - ${exception.runtimeType}');
|
||||
} catch (e) {
|
||||
debugPrint('🔴 Sentry error: Failed to capture exception - $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Capture a custom message
|
||||
///
|
||||
/// [message] - The message to capture
|
||||
/// [level] - Severity level (default: info)
|
||||
/// [params] - Optional parameters to include
|
||||
static Future<void> captureMessage(
|
||||
String message, {
|
||||
SentryLevel level = SentryLevel.info,
|
||||
Map<String, dynamic>? params,
|
||||
}) async {
|
||||
try {
|
||||
await Sentry.captureMessage(
|
||||
message,
|
||||
level: level,
|
||||
withScope: params != null
|
||||
? (scope) {
|
||||
params.forEach((key, value) {
|
||||
scope.setExtra(key, value);
|
||||
});
|
||||
}
|
||||
: null,
|
||||
);
|
||||
debugPrint('🔴 Sentry: Message captured - $message');
|
||||
} catch (e) {
|
||||
debugPrint('🔴 Sentry error: Failed to capture message - $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Set user context for error tracking
|
||||
///
|
||||
/// Call this after successful login to associate errors with users.
|
||||
///
|
||||
/// [userId] - User's unique identifier
|
||||
/// [email] - User's email (optional)
|
||||
/// [username] - User's display name (optional)
|
||||
/// [extras] - Additional user data (optional)
|
||||
static Future<void> setUser({
|
||||
required String userId,
|
||||
String? email,
|
||||
String? username,
|
||||
Map<String, dynamic>? extras,
|
||||
}) async {
|
||||
try {
|
||||
await Sentry.configureScope((scope) {
|
||||
scope.setUser(SentryUser(
|
||||
id: userId,
|
||||
email: email,
|
||||
username: username,
|
||||
data: extras,
|
||||
));
|
||||
});
|
||||
debugPrint('🔴 Sentry: User set - $userId');
|
||||
} catch (e) {
|
||||
debugPrint('🔴 Sentry error: Failed to set user - $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear user context on logout
|
||||
static Future<void> clearUser() async {
|
||||
try {
|
||||
await Sentry.configureScope((scope) {
|
||||
scope.setUser(null);
|
||||
});
|
||||
debugPrint('🔴 Sentry: User cleared');
|
||||
} catch (e) {
|
||||
debugPrint('🔴 Sentry error: Failed to clear user - $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Add a breadcrumb for tracking user actions
|
||||
///
|
||||
/// Breadcrumbs are used to track the sequence of events leading to an error.
|
||||
///
|
||||
/// [message] - Description of the action
|
||||
/// [category] - Category of the breadcrumb (e.g., 'navigation', 'ui.click')
|
||||
/// [data] - Additional data (optional)
|
||||
static Future<void> addBreadcrumb({
|
||||
required String message,
|
||||
String? category,
|
||||
Map<String, dynamic>? data,
|
||||
SentryLevel level = SentryLevel.info,
|
||||
}) async {
|
||||
try {
|
||||
await Sentry.addBreadcrumb(Breadcrumb(
|
||||
message: message,
|
||||
category: category,
|
||||
data: data,
|
||||
level: level,
|
||||
timestamp: DateTime.now(),
|
||||
));
|
||||
} catch (e) {
|
||||
debugPrint('🔴 Sentry error: Failed to add breadcrumb - $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Set a tag for filtering in Sentry dashboard
|
||||
///
|
||||
/// [key] - Tag name
|
||||
/// [value] - Tag value
|
||||
static Future<void> setTag(String key, String value) async {
|
||||
try {
|
||||
await Sentry.configureScope((scope) {
|
||||
scope.setTag(key, value);
|
||||
});
|
||||
} catch (e) {
|
||||
debugPrint('🔴 Sentry error: Failed to set tag - $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Set extra context data
|
||||
///
|
||||
/// [key] - Context key
|
||||
/// [value] - Context value (will be serialized)
|
||||
static Future<void> setExtra(String key, dynamic value) async {
|
||||
try {
|
||||
await Sentry.configureScope((scope) {
|
||||
scope.setExtra(key, value);
|
||||
});
|
||||
} catch (e) {
|
||||
debugPrint('🔴 Sentry error: Failed to set extra - $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Start a performance transaction
|
||||
///
|
||||
/// [name] - Transaction name
|
||||
/// [operation] - Operation type (e.g., 'http.client', 'ui.load')
|
||||
///
|
||||
/// Returns the transaction to be finished later.
|
||||
static ISentrySpan? startTransaction(String name, String operation) {
|
||||
try {
|
||||
return Sentry.startTransaction(name, operation);
|
||||
} catch (e) {
|
||||
debugPrint('🔴 Sentry error: Failed to start transaction - $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user