runable
This commit is contained in:
123
lib/core/errors/exceptions.dart
Normal file
123
lib/core/errors/exceptions.dart
Normal file
@@ -0,0 +1,123 @@
|
||||
/// Base class for all exceptions in the application
|
||||
/// Exceptions are thrown during runtime and should be caught and converted to failures
|
||||
abstract class AppException implements Exception {
|
||||
final String message;
|
||||
final String? code;
|
||||
|
||||
const AppException(this.message, {this.code});
|
||||
|
||||
@override
|
||||
String toString() => 'AppException: $message${code != null ? ' (Code: $code)' : ''}';
|
||||
}
|
||||
|
||||
/// Exception thrown when there's a server-related error
|
||||
/// This includes HTTP errors, API response errors, etc.
|
||||
class ServerException extends AppException {
|
||||
const ServerException(super.message, {super.code});
|
||||
|
||||
@override
|
||||
String toString() => 'ServerException: $message${code != null ? ' (Code: $code)' : ''}';
|
||||
}
|
||||
|
||||
/// Exception thrown when there's a network-related error
|
||||
/// This includes connection timeouts, no internet connection, etc.
|
||||
class NetworkException extends AppException {
|
||||
const NetworkException(super.message, {super.code});
|
||||
|
||||
@override
|
||||
String toString() => 'NetworkException: $message${code != null ? ' (Code: $code)' : ''}';
|
||||
}
|
||||
|
||||
/// Exception thrown when there's a local storage error
|
||||
/// This includes Hive errors, file system errors, etc.
|
||||
class CacheException extends AppException {
|
||||
const CacheException(super.message, {super.code});
|
||||
|
||||
@override
|
||||
String toString() => 'CacheException: $message${code != null ? ' (Code: $code)' : ''}';
|
||||
}
|
||||
|
||||
/// Exception thrown when input validation fails
|
||||
class ValidationException extends AppException {
|
||||
final Map<String, String>? fieldErrors;
|
||||
|
||||
const ValidationException(
|
||||
super.message, {
|
||||
super.code,
|
||||
this.fieldErrors,
|
||||
});
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
var result = 'ValidationException: $message${code != null ? ' (Code: $code)' : ''}';
|
||||
if (fieldErrors != null && fieldErrors!.isNotEmpty) {
|
||||
result += '\nField errors: ${fieldErrors.toString()}';
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// Exception thrown when a required permission is denied
|
||||
class PermissionException extends AppException {
|
||||
final String permissionType;
|
||||
|
||||
const PermissionException(
|
||||
super.message,
|
||||
this.permissionType, {
|
||||
super.code,
|
||||
});
|
||||
|
||||
@override
|
||||
String toString() =>
|
||||
'PermissionException: $message (Permission: $permissionType)${code != null ? ' (Code: $code)' : ''}';
|
||||
}
|
||||
|
||||
/// Exception thrown when scanning operation fails
|
||||
class ScannerException extends AppException {
|
||||
const ScannerException(super.message, {super.code});
|
||||
|
||||
@override
|
||||
String toString() => 'ScannerException: $message${code != null ? ' (Code: $code)' : ''}';
|
||||
}
|
||||
|
||||
/// Exception thrown when printing operation fails
|
||||
class PrintException extends AppException {
|
||||
const PrintException(super.message, {super.code});
|
||||
|
||||
@override
|
||||
String toString() => 'PrintException: $message${code != null ? ' (Code: $code)' : ''}';
|
||||
}
|
||||
|
||||
/// Exception thrown for JSON parsing errors
|
||||
class JsonException extends AppException {
|
||||
const JsonException(super.message, {super.code});
|
||||
|
||||
@override
|
||||
String toString() => 'JsonException: $message${code != null ? ' (Code: $code)' : ''}';
|
||||
}
|
||||
|
||||
/// Exception thrown for format-related errors (e.g., invalid barcode format)
|
||||
class FormatException extends AppException {
|
||||
final String expectedFormat;
|
||||
final String receivedFormat;
|
||||
|
||||
const FormatException(
|
||||
super.message,
|
||||
this.expectedFormat,
|
||||
this.receivedFormat, {
|
||||
super.code,
|
||||
});
|
||||
|
||||
@override
|
||||
String toString() =>
|
||||
'FormatException: $message (Expected: $expectedFormat, Received: $receivedFormat)${code != null ? ' (Code: $code)' : ''}';
|
||||
}
|
||||
|
||||
/// Generic exception for unexpected errors
|
||||
class UnknownException extends AppException {
|
||||
const UnknownException([super.message = 'An unexpected error occurred', String? code])
|
||||
: super(code: code);
|
||||
|
||||
@override
|
||||
String toString() => 'UnknownException: $message${code != null ? ' (Code: $code)' : ''}';
|
||||
}
|
||||
79
lib/core/errors/failures.dart
Normal file
79
lib/core/errors/failures.dart
Normal file
@@ -0,0 +1,79 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
/// Base class for all failures in the application
|
||||
/// Failures represent errors that can be handled gracefully
|
||||
abstract class Failure extends Equatable {
|
||||
final String message;
|
||||
|
||||
const Failure(this.message);
|
||||
|
||||
@override
|
||||
List<Object> get props => [message];
|
||||
}
|
||||
|
||||
/// Failure that occurs when there's a server-related error
|
||||
/// This includes HTTP errors, API errors, etc.
|
||||
class ServerFailure extends Failure {
|
||||
const ServerFailure(super.message);
|
||||
|
||||
@override
|
||||
String toString() => 'ServerFailure: $message';
|
||||
}
|
||||
|
||||
/// Failure that occurs when there's a network-related error
|
||||
/// This includes connection timeouts, no internet, etc.
|
||||
class NetworkFailure extends Failure {
|
||||
const NetworkFailure(super.message);
|
||||
|
||||
@override
|
||||
String toString() => 'NetworkFailure: $message';
|
||||
}
|
||||
|
||||
/// Failure that occurs when there's a local storage error
|
||||
/// This includes cache errors, database errors, etc.
|
||||
class CacheFailure extends Failure {
|
||||
const CacheFailure(super.message);
|
||||
|
||||
@override
|
||||
String toString() => 'CacheFailure: $message';
|
||||
}
|
||||
|
||||
/// Failure that occurs when input validation fails
|
||||
class ValidationFailure extends Failure {
|
||||
const ValidationFailure(super.message);
|
||||
|
||||
@override
|
||||
String toString() => 'ValidationFailure: $message';
|
||||
}
|
||||
|
||||
/// Failure that occurs when a required permission is denied
|
||||
class PermissionFailure extends Failure {
|
||||
const PermissionFailure(super.message);
|
||||
|
||||
@override
|
||||
String toString() => 'PermissionFailure: $message';
|
||||
}
|
||||
|
||||
/// Failure that occurs when scanning operation fails
|
||||
class ScannerFailure extends Failure {
|
||||
const ScannerFailure(super.message);
|
||||
|
||||
@override
|
||||
String toString() => 'ScannerFailure: $message';
|
||||
}
|
||||
|
||||
/// Failure that occurs when printing operation fails
|
||||
class PrintFailure extends Failure {
|
||||
const PrintFailure(super.message);
|
||||
|
||||
@override
|
||||
String toString() => 'PrintFailure: $message';
|
||||
}
|
||||
|
||||
/// Generic failure for unexpected errors
|
||||
class UnknownFailure extends Failure {
|
||||
const UnknownFailure([super.message = 'An unexpected error occurred']);
|
||||
|
||||
@override
|
||||
String toString() => 'UnknownFailure: $message';
|
||||
}
|
||||
Reference in New Issue
Block a user