networking and error handling
This commit is contained in:
@@ -1,8 +1,15 @@
|
||||
class AppException implements Exception {
|
||||
final String message;
|
||||
final String prefix;
|
||||
final int? statusCode;
|
||||
final dynamic originalError;
|
||||
|
||||
AppException([this.message = 'An unknown error occurred.', this.prefix = '']);
|
||||
AppException([
|
||||
this.message = 'An unknown error occurred.',
|
||||
this.prefix = '',
|
||||
this.statusCode,
|
||||
this.originalError,
|
||||
]);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
@@ -11,11 +18,91 @@ class AppException implements Exception {
|
||||
}
|
||||
|
||||
class FetchDataException extends AppException {
|
||||
FetchDataException([String? message])
|
||||
: super(message ?? 'Error During Communication: ', 'Network Error');
|
||||
FetchDataException([String? message, int? statusCode, originalError])
|
||||
: super(
|
||||
message ?? 'Error During Communication: ',
|
||||
'Network Error',
|
||||
statusCode,
|
||||
originalError,
|
||||
);
|
||||
}
|
||||
|
||||
class AuthenticationException extends AppException {
|
||||
AuthenticationException([String? message])
|
||||
: super(message ?? 'Authentication Failed.', 'Auth Error');
|
||||
AuthenticationException([String? message, int? statusCode, originalError])
|
||||
: super(
|
||||
message ?? 'Authentication Failed.',
|
||||
'Auth Error: ',
|
||||
statusCode,
|
||||
originalError,
|
||||
);
|
||||
}
|
||||
|
||||
class NetworkConnectionException extends AppException {
|
||||
NetworkConnectionException([String? message, originalError])
|
||||
: super(
|
||||
message ?? 'No internet connection available.',
|
||||
'Connection Error: ',
|
||||
null,
|
||||
originalError,
|
||||
);
|
||||
}
|
||||
|
||||
class TimeoutException extends AppException {
|
||||
TimeoutException([String? message, originalError])
|
||||
: super(
|
||||
message ?? 'Request timed out.',
|
||||
'Timeout Error: ',
|
||||
null,
|
||||
originalError,
|
||||
);
|
||||
}
|
||||
|
||||
class ServerException extends AppException {
|
||||
ServerException([String? message, int? statusCode, originalError])
|
||||
: super(
|
||||
message ?? 'Server error occurred.',
|
||||
'Server Error: ',
|
||||
statusCode,
|
||||
originalError,
|
||||
);
|
||||
}
|
||||
|
||||
class BadRequestException extends AppException {
|
||||
BadRequestException([String? message, int? statusCode, originalError])
|
||||
: super(
|
||||
message ?? 'Bad request.',
|
||||
'Bad Request Error: ',
|
||||
statusCode,
|
||||
originalError,
|
||||
);
|
||||
}
|
||||
|
||||
class NotFoundException extends AppException {
|
||||
NotFoundException([String? message, int? statusCode, originalError])
|
||||
: super(
|
||||
message ?? 'Resource not found.',
|
||||
'Not Found Error: ',
|
||||
statusCode,
|
||||
originalError,
|
||||
);
|
||||
}
|
||||
|
||||
class ForbiddenException extends AppException {
|
||||
ForbiddenException([String? message, int? statusCode, originalError])
|
||||
: super(
|
||||
message ?? 'Access forbidden.',
|
||||
'Forbidden Error: ',
|
||||
statusCode,
|
||||
originalError,
|
||||
);
|
||||
}
|
||||
|
||||
class RateLimitException extends AppException {
|
||||
RateLimitException([String? message, int? statusCode, originalError])
|
||||
: super(
|
||||
message ?? 'Rate limit exceeded.',
|
||||
'Rate Limit Error: ',
|
||||
statusCode,
|
||||
originalError,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'dart:io';
|
||||
|
||||
class NetworkConnectivity {
|
||||
static final NetworkConnectivity _instance = NetworkConnectivity._internal();
|
||||
factory NetworkConnectivity() => _instance;
|
||||
NetworkConnectivity._internal();
|
||||
|
||||
/// Check if device has internet connectivity
|
||||
Future<bool> hasInternetConnection() async {
|
||||
try {
|
||||
// Try to reach a reliable host to check internet connectivity
|
||||
final result = await InternetAddress.lookup('google.com');
|
||||
return result.isNotEmpty && result[0].rawAddress.isNotEmpty;
|
||||
} on SocketException catch (_) {
|
||||
return false;
|
||||
} on Exception catch (_) {
|
||||
// If connectivity check fails, assume no connection
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user