Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor api exceptions #70

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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