CustomException.fromDioException constructor

CustomException.fromDioException(
  1. Exception error
)

Implementation

factory CustomException.fromDioException(Exception error) {
  try {
    if (error is DioException) {
      switch (error.type) {
        case DioExceptionType.cancel:
          return CustomException(
            exceptionType: _ExceptionType.CancelException,
            statusCode: error.response?.statusCode,
            message: 'Request cancelled prematurely',
          );
        case DioExceptionType.connectionTimeout:
          return CustomException(
            exceptionType: _ExceptionType.ConnectTimeoutException,
            statusCode: error.response?.statusCode,
            message: 'Connection not established',
          );
        case DioExceptionType.sendTimeout:
          return CustomException(
            exceptionType: _ExceptionType.SendTimeoutException,
            statusCode: error.response?.statusCode,
            message: 'Failed to send',
          );
        case DioExceptionType.receiveTimeout:
          return CustomException(
            exceptionType: _ExceptionType.ReceiveTimeoutException,
            statusCode: error.response?.statusCode,
            message: 'Failed to receive',
          );
        case DioExceptionType.badResponse:
        case DioExceptionType.unknown:
          if (error.message?.contains(_ExceptionType.SocketException.name) ??
              false) {
            return CustomException(
              exceptionType: _ExceptionType.FetchDataException,
              statusCode: error.response?.statusCode,
              message: 'No internet connectivity',
            );
          }
          final data = error.response?.data as Map<String, dynamic>;
          final headers = data['headers'] as Map<String, dynamic>;

          if (headers['code'] == null) {
            return CustomException(
              exceptionType: _ExceptionType.UnrecognizedException,
              statusCode: error.response?.statusCode,
              message: error.response?.statusMessage ?? 'Unknown',
            );
          }
          final String name = headers['code'] as String;
          final String message = headers['message'] as String;
          if (name == _ExceptionType.TokenExpiredException.name) {
            return CustomException(
              exceptionType: _ExceptionType.TokenExpiredException,
              code: name,
              statusCode: error.response?.statusCode,
              message: message,
            );
          }
          return CustomException(
            message: message,
            code: name,
            statusCode: error.response?.statusCode,
          );
        case DioExceptionType.badCertificate:
          return CustomException(
            exceptionType: _ExceptionType.ReceiveTimeoutException,
            statusCode: error.response?.statusCode,
            message: 'Bad Certificate',
          );
        case DioExceptionType.connectionError:
          return CustomException(
            exceptionType: _ExceptionType.ReceiveTimeoutException,
            statusCode: error.response?.statusCode,
            message: 'Connection Error',
          );
      }
    } else {
      return CustomException(
        exceptionType: _ExceptionType.UnrecognizedException,
        message: 'Error unrecognized',
      );
    }
  } on FormatException catch (e) {
    return CustomException(
      exceptionType: _ExceptionType.FormatException,
      message: e.message,
    );
  } on Exception catch (_) {
    return CustomException(
      exceptionType: _ExceptionType.UnrecognizedException,
      message: 'Error unrecognized',
    );
  }
}