Line data Source code
1 : import 'dart:convert'; 2 : 3 : /// Represents an API failure. 4 : /// 5 : /// This class is used to encapsulate the details of an API failure, including an error code and a description of the error. 6 : /// 7 : /// **Properties**: 8 : /// - [String] `code`: A string representing the error code associated with the failure. This code can be used to identify the type of error that occurred. 9 : /// - [String] `description`: A string providing a description of the failure. This message typically contains information about why the failure occurred or additional details for debugging. 10 : /// 11 : /// **Constructor**: 12 : /// - `ApiFailure({required this.code, required this.description})`: Initializes an instance of [ApiFailure] with the provided error code and description. 13 : /// 14 : /// **Notes**: 15 : /// - This class is commonly used in error handling within API responses. The `code` and `description` fields can be used to provide detailed information about errors and to handle different types of failures appropriately. 16 : /// 17 : /// **Example**: 18 : /// ```dart 19 : /// ApiFailure failure = ApiFailure( 20 : /// code: '404', 21 : /// description: 'Resource not found', 22 : /// ); 23 : /// print('Error Code: ${failure.code}'); 24 : /// print('Error Description: ${failure.description}'); 25 : /// ``` 26 : class ApiFailure { 27 2 : ApiFailure({ 28 : this.status = '', 29 : this.code = 0, 30 : this.description = '', 31 : this.message = '', 32 : }); 33 : 34 : final int code; 35 : final String status; 36 : final String description; 37 : final String message; 38 : 39 2 : factory ApiFailure.fromJson(String str) => 40 4 : ApiFailure.fromMap(json.decode(str)); 41 : 42 4 : factory ApiFailure.fromMap(Map<String, dynamic> json) => ApiFailure( 43 2 : code: json["code"] ?? 0, 44 2 : status: json["status"] ?? '', 45 2 : message: json["message"] ?? '', 46 2 : description: json["description"] ?? '', 47 : ); 48 : }