handleException<T extends Object> method
Implementation
Future<Either<AppException, response.Response>>
handleException<T extends Object>(
Future<Response<dynamic>> Function() handler,
{String endpoint = ''}) async {
try {
String? message;
final res = await handler();
if (res.data is List) {
} else {
print(res.data.runtimeType);
if (res.data.runtimeType == String) {
message = res.data;
} else {
message =
Map<String, dynamic>.from(res.data as Map)['message'].toString();
}
}
return Right(
response.Response(
statusCode: res.statusCode ?? 200,
data: res.data,
statusMessage: message ?? res.statusMessage,
),
);
} catch (e) {
String message = '';
String identifier = '';
int statusCode = 0;
log(e.runtimeType.toString());
switch (e.runtimeType) {
case SocketException:
e as SocketException;
message = 'Unable to connect to the server.';
statusCode = 0;
identifier = 'Socket Exception ${e.message}\n at $endpoint';
break;
case DioException:
e as DioException;
var msg = e.response?.data?['message'] as String?;
var errorMsg = e.response?.data?['errors'] as String?;
errorMsg ??= '';
var expMsg = e.message;
message = msg != null && msg.isNotEmpty
? msg
: expMsg != null && expMsg.isNotEmpty
? expMsg
: 'Internal Error occured';
statusCode = 1;
identifier = 'DioException ${e.message} \nat $endpoint';
msg = msg ?? '' + '\n' + errorMsg;
break;
default:
message = 'Unknown error occured';
statusCode = 2;
identifier = 'Unknown error ${e.toString()}\n at $endpoint';
}
return Left(
AppException(
message: message,
statusCode: statusCode,
identifier: identifier,
),
);
}
}