add auth, format

This commit is contained in:
Phuoc Nguyen
2025-11-07 11:52:06 +07:00
parent 24a8508fce
commit 3803bd26e0
173 changed files with 8505 additions and 7116 deletions

View File

@@ -10,35 +10,27 @@ library;
/// Base exception for all network-related errors
class NetworkException implements Exception {
const NetworkException(
this.message, {
this.statusCode,
this.data,
});
const NetworkException(this.message, {this.statusCode, this.data});
final String message;
final int? statusCode;
final dynamic data;
@override
String toString() => 'NetworkException: $message${statusCode != null ? ' (Status: $statusCode)' : ''}';
String toString() =>
'NetworkException: $message${statusCode != null ? ' (Status: $statusCode)' : ''}';
}
/// Exception thrown when there's no internet connection
class NoInternetException extends NetworkException {
const NoInternetException()
: super(
'Không có kết nối internet. Vui lòng kiểm tra kết nối của bạn.',
);
: super('Không có kết nối internet. Vui lòng kiểm tra kết nối của bạn.');
}
/// Exception thrown when connection times out
class TimeoutException extends NetworkException {
const TimeoutException()
: super(
'Kết nối quá lâu. Vui lòng thử lại.',
statusCode: 408,
);
: super('Kết nối quá lâu. Vui lòng thử lại.', statusCode: 408);
}
/// Exception thrown when server returns 500+ errors
@@ -52,10 +44,7 @@ class ServerException extends NetworkException {
/// Exception thrown when server is unreachable
class ServiceUnavailableException extends ServerException {
const ServiceUnavailableException()
: super(
'Dịch vụ tạm thời không khả dụng. Vui lòng thử lại sau.',
503,
);
: super('Dịch vụ tạm thời không khả dụng. Vui lòng thử lại sau.', 503);
}
// ============================================================================
@@ -64,10 +53,7 @@ class ServiceUnavailableException extends ServerException {
/// Base exception for authentication-related errors
class AuthException implements Exception {
const AuthException(
this.message, {
this.statusCode,
});
const AuthException(this.message, {this.statusCode});
final String message;
final int? statusCode;
@@ -79,10 +65,7 @@ class AuthException implements Exception {
/// Exception thrown when authentication credentials are invalid
class InvalidCredentialsException extends AuthException {
const InvalidCredentialsException()
: super(
'Thông tin đăng nhập không hợp lệ.',
statusCode: 401,
);
: super('Thông tin đăng nhập không hợp lệ.', statusCode: 401);
}
/// Exception thrown when user is not authenticated
@@ -95,46 +78,37 @@ class UnauthorizedException extends AuthException {
/// Exception thrown when user doesn't have permission
class ForbiddenException extends AuthException {
const ForbiddenException()
: super(
'Bạn không có quyền truy cập tài nguyên này.',
statusCode: 403,
);
: super('Bạn không có quyền truy cập tài nguyên này.', statusCode: 403);
}
/// Exception thrown when auth token is expired
class TokenExpiredException extends AuthException {
const TokenExpiredException()
: super(
'Phiên đăng nhập hết hạn. Vui lòng đăng nhập lại.',
statusCode: 401,
);
: super(
'Phiên đăng nhập hết hạn. Vui lòng đăng nhập lại.',
statusCode: 401,
);
}
/// Exception thrown when refresh token is invalid
class InvalidRefreshTokenException extends AuthException {
const InvalidRefreshTokenException()
: super(
'Không thể làm mới phiên đăng nhập. Vui lòng đăng nhập lại.',
statusCode: 401,
);
: super(
'Không thể làm mới phiên đăng nhập. Vui lòng đăng nhập lại.',
statusCode: 401,
);
}
/// Exception thrown when OTP is invalid
class InvalidOTPException extends AuthException {
const InvalidOTPException()
: super(
'Mã OTP không hợp lệ. Vui lòng thử lại.',
statusCode: 400,
);
: super('Mã OTP không hợp lệ. Vui lòng thử lại.', statusCode: 400);
}
/// Exception thrown when OTP is expired
class OTPExpiredException extends AuthException {
const OTPExpiredException()
: super(
'Mã OTP đã hết hạn. Vui lòng yêu cầu mã mới.',
statusCode: 400,
);
: super('Mã OTP đã hết hạn. Vui lòng yêu cầu mã mới.', statusCode: 400);
}
// ============================================================================
@@ -143,10 +117,7 @@ class OTPExpiredException extends AuthException {
/// Exception thrown when request data is invalid
class ValidationException implements Exception {
const ValidationException(
this.message, {
this.errors,
});
const ValidationException(this.message, {this.errors});
final String message;
final Map<String, List<String>>? errors;
@@ -198,9 +169,7 @@ class NotFoundException implements Exception {
/// Exception thrown when trying to create a duplicate resource
class ConflictException implements Exception {
const ConflictException([
this.message = 'Tài nguyên đã tồn tại.',
]);
const ConflictException([this.message = 'Tài nguyên đã tồn tại.']);
final String message;
@@ -237,10 +206,7 @@ class RateLimitException implements Exception {
/// Exception thrown for payment-related errors
class PaymentException implements Exception {
const PaymentException(
this.message, {
this.transactionId,
});
const PaymentException(this.message, {this.transactionId});
final String message;
final String? transactionId;
@@ -259,8 +225,7 @@ class PaymentFailedException extends PaymentException {
/// Exception thrown when payment is cancelled
class PaymentCancelledException extends PaymentException {
const PaymentCancelledException()
: super('Thanh toán đã bị hủy.');
const PaymentCancelledException() : super('Thanh toán đã bị hủy.');
}
// ============================================================================
@@ -269,9 +234,7 @@ class PaymentCancelledException extends PaymentException {
/// Exception thrown for cache-related errors
class CacheException implements Exception {
const CacheException([
this.message = 'Lỗi khi truy cập bộ nhớ đệm.',
]);
const CacheException([this.message = 'Lỗi khi truy cập bộ nhớ đệm.']);
final String message;
@@ -281,8 +244,7 @@ class CacheException implements Exception {
/// Exception thrown when cache data is corrupted
class CacheCorruptedException extends CacheException {
const CacheCorruptedException()
: super('Dữ liệu bộ nhớ đệm bị hỏng.');
const CacheCorruptedException() : super('Dữ liệu bộ nhớ đệm bị hỏng.');
}
// ============================================================================
@@ -291,9 +253,7 @@ class CacheCorruptedException extends CacheException {
/// Exception thrown for local storage errors
class StorageException implements Exception {
const StorageException([
this.message = 'Lỗi khi truy cập bộ nhớ cục bộ.',
]);
const StorageException([this.message = 'Lỗi khi truy cập bộ nhớ cục bộ.']);
final String message;
@@ -304,7 +264,7 @@ class StorageException implements Exception {
/// Exception thrown when storage is full
class StorageFullException extends StorageException {
const StorageFullException()
: super('Bộ nhớ đã đầy. Vui lòng giải phóng không gian.');
: super('Bộ nhớ đã đầy. Vui lòng giải phóng không gian.');
}
// ============================================================================