1/// API Exception Types 2/// 3/// Custom exception classes for different types of API failures. 4/// This allows better error handling and user-friendly error messages. 5library; 6 7import 'package:dio/dio.dart'; 8 9/// Base class for all API exceptions 10class ApiException implements Exception { 11 ApiException(this.message, {this.statusCode, this.originalError}); 12 13 /// Create ApiException from DioException 14 factory ApiException.fromDioError(DioException error) { 15 switch (error.type) { 16 case DioExceptionType.connectionTimeout: 17 case DioExceptionType.sendTimeout: 18 case DioExceptionType.receiveTimeout: 19 return NetworkException( 20 'Request timeout. Please check your connection.', 21 originalError: error, 22 ); 23 case DioExceptionType.badResponse: 24 final statusCode = error.response?.statusCode; 25 final message = 26 error.response?.data?['message'] as String? ?? 27 error.response?.data?['error'] as String? ?? 28 'Server error'; 29 30 if (statusCode == 401) { 31 return AuthenticationException(message, originalError: error); 32 } else if (statusCode == 404) { 33 return NotFoundException(message, originalError: error); 34 } else if (statusCode != null && statusCode >= 500) { 35 return ServerException( 36 message, 37 statusCode: statusCode, 38 originalError: error, 39 ); 40 } 41 return ApiException( 42 message, 43 statusCode: statusCode, 44 originalError: error, 45 ); 46 case DioExceptionType.cancel: 47 return ApiException('Request was cancelled', originalError: error); 48 case DioExceptionType.connectionError: 49 return NetworkException( 50 'Connection failed. Please check your internet.', 51 originalError: error, 52 ); 53 case DioExceptionType.badCertificate: 54 return NetworkException('SSL certificate error', originalError: error); 55 case DioExceptionType.unknown: 56 return NetworkException('Network error occurred', originalError: error); 57 } 58 } 59 final String message; 60 final int? statusCode; 61 final dynamic originalError; 62 63 @override 64 String toString() => message; 65} 66 67/// Authentication failure (401) 68/// Token expired, invalid, or missing 69class AuthenticationException extends ApiException { 70 AuthenticationException(super.message, {super.originalError}) 71 : super(statusCode: 401); 72} 73 74/// Resource not found (404) 75/// PDS, community, post, or user not found 76class NotFoundException extends ApiException { 77 NotFoundException(super.message, {super.originalError}) 78 : super(statusCode: 404); 79} 80 81/// Server error (500+) 82/// Backend or PDS server failure 83class ServerException extends ApiException { 84 ServerException(super.message, {super.statusCode, super.originalError}); 85} 86 87/// Network connectivity failure 88/// No internet, connection refused, timeout 89class NetworkException extends ApiException { 90 NetworkException(super.message, {super.originalError}) 91 : super(statusCode: null); 92} 93 94/// Federation error 95/// atProto PDS unreachable or DID resolution failure 96class FederationException extends ApiException { 97 FederationException(super.message, {super.originalError}) 98 : super(statusCode: null); 99} 100 101/// Validation error 102/// Client-side validation failure (empty content, exceeds limits, etc.) 103class ValidationException extends ApiException { 104 ValidationException(super.message) : super(statusCode: null); 105}