Skip to content

Commit

Permalink
Merge pull request #70 from devsadeq/feature/advance_refactor
Browse files Browse the repository at this point in the history
refactor api exceptions
  • Loading branch information
devsadeq authored Nov 7, 2022
2 parents 8cc40a6 + 8986828 commit 9ddc5af
Showing 1 changed file with 41 additions and 48 deletions.
89 changes: 41 additions & 48 deletions lib/app/data/remote/exceptions/dio_exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,50 @@ import 'package:dio/dio.dart';
class DioExceptions implements Exception {
late String message;

static const Map<String, String> _errorCodes = {
'400': 'Bad Request',
'401': 'Unauthorized',
'403': 'Forbidden',
'404': 'Not Found',
'408': 'Request Timeout',
'500': 'Internal Server Error',
'502': 'Bad Gateway',
'503': 'Service Unavailable',
'504': 'Gateway Timeout',
'505': 'HTTP Version Not Supported',
'511': 'Network Authentication Required',
'520': 'Unknown Error',
'521': 'Web Server Is Down',
'522': 'Connection Timed Out',
'523': 'Origin Is Unreachable',
'524': 'A Timeout Occurred',
'525': 'SSL Handshake Failed',
'526': 'Invalid SSL Certificate',
'530': 'Origin DNS Error',
'598': 'Network Read Timeout Error',
'599': 'Network Connect Timeout Error',
};

static const Map<String, String> _errorTypes = {
'cancel': 'Request to API server was cancelled',
'connectTimeout': 'Connection timeout with API server',
'default': 'Connection to API server failed due to internet connection',
'receiveTimeout': 'Receive timeout in connection with API server',
'sendTimeout': 'Send timeout in connection with API server',
'other': 'Connection to API server failed due to internet connection',
};

DioExceptions.fromDioError(DioError dioError) {
switch (dioError.type) {
case DioErrorType.cancel:
message = "Request to API server was cancelled";
break;
case DioErrorType.connectTimeout:
message = "Connection timeout with API server";
break;
case DioErrorType.receiveTimeout:
message = "Receive timeout in connection with API server";
break;
case DioErrorType.response:
message = _handleError(
dioError.response?.statusCode,
dioError.response?.data,
);
break;
case DioErrorType.sendTimeout:
message = "Send timeout in connection with API server";
break;
case DioErrorType.other:
if (dioError.message.contains("SocketException")) {
message = 'No Internet';
break;
}
message = "Unexpected error occurred";
break;
default:
message = "Something went wrong";
break;
}
message = _getErrorMessage(dioError);
}

String _handleError(int? statusCode, dynamic error) {
switch (statusCode) {
case 400:
// return 'Bad request';
return error['error'];
case 401:
return 'Unauthorized';
case 403:
return 'Forbidden';
case 404:
return error['error'];
case 500:
return 'Internal server error';
case 502:
return 'Bad gateway';
default:
return 'Oops something went wrong';
}
String _getErrorMessage(DioError error) {
String errorType = error.type.toString().split('.').last;
String errorMessage = _errorTypes[errorType] ??
error.response?.data['error'] ??
_errorCodes[error.response?.statusCode.toString()] ??
'Something went wrong';
return errorMessage;
}

@override
Expand Down

0 comments on commit 9ddc5af

Please sign in to comment.