Skip to content

Instantly share code, notes, and snippets.

@ashishrawat2911
Created July 10, 2020 15:37
Show Gist options
  • Save ashishrawat2911/1dcfde5d713f389dcebbd9a69b863145 to your computer and use it in GitHub Desktop.
Save ashishrawat2911/1dcfde5d713f389dcebbd9a69b863145 to your computer and use it in GitHub Desktop.
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'network_exceptions.freezed.dart';
@freezed
abstract class NetworkExceptions with _$NetworkExceptions {
const factory NetworkExceptions.requestCancelled() = RequestCancelled;
const factory NetworkExceptions.unauthorisedRequest() = UnauthorisedRequest;
const factory NetworkExceptions.badRequest() = BadRequest;
const factory NetworkExceptions.notFound(String reason) = NotFound;
const factory NetworkExceptions.methodNotAllowed() = MethodNotAllowed;
const factory NetworkExceptions.notAcceptable() = NotAcceptable;
const factory NetworkExceptions.requestTimeout() = RequestTimeout;
const factory NetworkExceptions.sendTimeout() = SendTimeout;
const factory NetworkExceptions.conflict() = Conflict;
const factory NetworkExceptions.internalServerError() = InternalServerError;
const factory NetworkExceptions.notImplemented() = NotImplemented;
const factory NetworkExceptions.serviceUnavailable() = ServiceUnavailable;
const factory NetworkExceptions.noInternetConnection() = NoInternetConnection;
const factory NetworkExceptions.formatException() = FormatException;
const factory NetworkExceptions.unableToProcess() = UnableToProcess;
const factory NetworkExceptions.defaultError(String error) = DefaultError;
const factory NetworkExceptions.unexpectedError() = UnexpectedError;
static NetworkExceptions getDioException(error) {
if (error is Exception) {
try {
NetworkExceptions networkExceptions;
if (error is DioError) {
switch (error.type) {
case DioErrorType.CANCEL:
networkExceptions = NetworkExceptions.requestCancelled();
break;
case DioErrorType.CONNECT_TIMEOUT:
networkExceptions = NetworkExceptions.requestTimeout();
break;
case DioErrorType.DEFAULT:
networkExceptions = NetworkExceptions.noInternetConnection();
break;
case DioErrorType.RECEIVE_TIMEOUT:
networkExceptions = NetworkExceptions.sendTimeout();
break;
case DioErrorType.RESPONSE:
switch (error.response.statusCode) {
case 400:
networkExceptions = NetworkExceptions.unauthorisedRequest();
break;
case 401:
networkExceptions = NetworkExceptions.unauthorisedRequest();
break;
case 403:
networkExceptions = NetworkExceptions.unauthorisedRequest();
break;
case 404:
networkExceptions = NetworkExceptions.notFound("Not found");
break;
case 409:
networkExceptions = NetworkExceptions.conflict();
break;
case 408:
networkExceptions = NetworkExceptions.requestTimeout();
break;
case 500:
networkExceptions = NetworkExceptions.internalServerError();
break;
case 503:
networkExceptions = NetworkExceptions.serviceUnavailable();
break;
default:
var responseCode = error.response.statusCode;
networkExceptions = NetworkExceptions.defaultError(
"Received invalid status code: $responseCode",
);
}
break;
case DioErrorType.SEND_TIMEOUT:
networkExceptions = NetworkExceptions.sendTimeout();
break;
}
} else if (error is SocketException) {
networkExceptions = NetworkExceptions.noInternetConnection();
} else {
networkExceptions = NetworkExceptions.unexpectedError();
}
return networkExceptions;
} on FormatException catch (e) {
// Helper.printError(e.toString());
return NetworkExceptions.formatException();
} catch (_) {
return NetworkExceptions.unexpectedError();
}
} else {
if (error.toString().contains("is not a subtype of")) {
return NetworkExceptions.unableToProcess();
} else {
return NetworkExceptions.unexpectedError();
}
}
}
static String getErrorMessage(NetworkExceptions networkExceptions) {
var errorMessage = "";
networkExceptions.when(notImplemented: () {
errorMessage = "Not Implemented";
}, requestCancelled: () {
errorMessage = "Request Cancelled";
}, internalServerError: () {
errorMessage = "Internal Server Error";
}, notFound: (String reason) {
errorMessage = reason;
}, serviceUnavailable: () {
errorMessage = "Service unavailable";
}, methodNotAllowed: () {
errorMessage = "Method Allowed";
}, badRequest: () {
errorMessage = "Bad request";
}, unauthorisedRequest: () {
errorMessage = "Unauthorised request";
}, unexpectedError: () {
errorMessage = "Unexpected error occurred";
}, requestTimeout: () {
errorMessage = "Connection request timeout";
}, noInternetConnection: () {
errorMessage = "No internet connection";
}, conflict: () {
errorMessage = "Error due to a conflict";
}, sendTimeout: () {
errorMessage = "Send timeout in connection with API server";
}, unableToProcess: () {
errorMessage = "Unable to process the data";
}, defaultError: (String error) {
errorMessage = error;
}, formatException: () {
errorMessage = "Unexpected error occurred";
}, notAcceptable: () {
errorMessage = "Not acceptable";
});
return errorMessage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment