safeFetch<T> method
Implementation
@protected
Future<Either<FetchFailure, T>> safeFetch<T>(
Future<T> Function() call) async {
return safeCall(
call: call,
onNetworkError: () => const FetchFailure.networkError(),
onResponseError: (Response? response) {
try {
if (response != null && response.statusCode != null) {
int? statusCode = response.statusCode ?? 00;
var jsonString = json.encode(response.data["errors"]);
var msg = List<String>.from(json.decode(jsonString));
var data = json.encode(response.data["data"]);
var codeName = response.data["code_name"];
if (statusCode == 401) {
return FetchFailure.login(msg.toList(), data, codeName);
} else if (statusCode >= 400 && statusCode < 500) {
try {
return FetchFailure.warningError(statusCode, msg, codeName);
} catch (e) {
msg = [response.statusMessage.toString()];
data = response.data.toString();
return FetchFailure.warningError(statusCode, msg, codeName);
}
} else if (statusCode >= 500 && statusCode < 600) {
try {
return FetchFailure.serverError(statusCode, msg, codeName);
} catch (e) {
msg = [response.statusMessage.toString()];
var error = response.data.toString();
return FetchFailure.warningError(statusCode, msg, error);
}
}
}
return FetchFailure.unknownError(101, "Unknown", "Unknown");
} catch (e) {
var status = response?.statusCode ?? 101;
String msg = response?.statusMessage ?? "";
var error = response?.statusMessage ?? "";
return FetchFailure.serverError(status, [msg], error);
}
},
onUnknownError: (Exception e) =>
FetchFailure.unknownError(101, "Unknown", "Unknown"),
);
}