classifyException function

ApiError classifyException(
  1. Object error
)

Classify an exception (connection error, timeout, etc.) into an ApiError.

Implementation

ApiError classifyException(Object error) {
  final msg = error.toString().toLowerCase();

  if (msg.contains('timeout') || msg.contains('timed out')) {
    return ApiError(
      type: ApiErrorType.connectionTimeout,
      message: apiTimeoutErrorMessage,
    );
  }

  if (msg.contains('connection reset') ||
      msg.contains('econnreset') ||
      msg.contains('broken pipe')) {
    return ApiError(
      type: ApiErrorType.connectionReset,
      message: 'Connection reset. Retrying...',
    );
  }

  if (msg.contains('certificate') ||
      msg.contains('ssl') ||
      msg.contains('tls')) {
    return ApiError(
      type: ApiErrorType.sslError,
      message: 'SSL/TLS certificate error.',
    );
  }

  return ApiError(type: ApiErrorType.unknown, message: error.toString());
}