diff --git a/lib/common/api/api_client.dart b/lib/common/api/api_client.dart index ca71634..74f74f2 100644 --- a/lib/common/api/api_client.dart +++ b/lib/common/api/api_client.dart @@ -1,3 +1,4 @@ +// ignore: depend_on_referenced_packages import 'package:awesome_dio_interceptor/awesome_dio_interceptor.dart'; import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; @@ -14,6 +15,7 @@ import 'errors/unauthorized_error.dart'; import 'interceptors/bad_network_interceptor.dart'; import 'interceptors/bad_request_interceptor.dart'; import 'interceptors/connection_timeout_interceptor.dart'; +import 'interceptors/crashlytic_interceptor.dart'; import 'interceptors/internal_server_interceptor.dart'; import 'interceptors/not_found_interceptor.dart'; import 'interceptors/unauthorized_interceptor.dart'; @@ -32,6 +34,7 @@ class ApiClient { _dio.interceptors.add(NotFoundErrorInterceptor()); _dio.interceptors.add(UnauthorizedInterceptor()); _dio.interceptors.add(ConnectionTimeoutErrorInterceptor()); + _dio.interceptors.add(CrashlyticsInterceptor()); if (kDebugMode) { _dio.interceptors.add( diff --git a/lib/common/api/interceptors/crashlytic_interceptor.dart b/lib/common/api/interceptors/crashlytic_interceptor.dart new file mode 100644 index 0000000..969c063 --- /dev/null +++ b/lib/common/api/interceptors/crashlytic_interceptor.dart @@ -0,0 +1,51 @@ +import 'package:dio/dio.dart'; +import 'package:firebase_crashlytics/firebase_crashlytics.dart'; + +class CrashlyticsInterceptor extends Interceptor { + @override + void onError(DioException err, ErrorInterceptorHandler handler) { + // Log semua error ke Crashlytics + FirebaseCrashlytics.instance.recordError( + err, + err.stackTrace, + reason: _getErrorReason(err), + information: _getErrorInformation(err), + fatal: false, + ); + + super.onError(err, handler); + } + + String _getErrorReason(DioException err) { + final statusCode = err.response?.statusCode; + + if (statusCode != null) { + return 'HTTP $statusCode Error: ${err.requestOptions.uri.path}'; + } + + switch (err.type) { + case DioExceptionType.connectionTimeout: + return 'Connection Timeout'; + case DioExceptionType.sendTimeout: + return 'Send Timeout'; + case DioExceptionType.receiveTimeout: + return 'Receive Timeout'; + case DioExceptionType.connectionError: + return 'Connection Error'; + case DioExceptionType.cancel: + return 'Request Cancelled'; + default: + return 'Network Error: ${err.type.name}'; + } + } + + List _getErrorInformation(DioException err) { + return [ + 'URL: ${err.requestOptions.uri}', + 'Method: ${err.requestOptions.method}', + 'Status Code: ${err.response?.statusCode ?? 'N/A'}', + 'Error Type: ${err.type.name}', + 'Message: ${err.message ?? 'No message'}', + ]; + } +}