
This commit improves the application's robustness by enhancing data handling and adding error logging. Specifically, it: - Updates the project version to 1.0.4 in `pyproject.toml` and `setup.py`. - Removes redis from requirements.txt - Improves data parsing in `KeyService.parse_hash_data` to handle None values and simplify the logic. - Adds error logging to `KeyService.parse_hash_data` and `KeyService.process_list_values` to catch and log parsing errors. - Modifies `KeyService.process_list_values` to ensure the return value is always a list. - Adds try-except blocks in `get_keys` and `get_key_value` routes to handle potential exceptions during key processing and log warnings.
83 lines
2.3 KiB
Python
83 lines
2.3 KiB
Python
from .logger import logger
|
|
|
|
class KeyService:
|
|
@staticmethod
|
|
def parse_hash_data(hash_data):
|
|
"""
|
|
Process hash data from Redis.
|
|
|
|
Args:
|
|
hash_data: Hash data from Redis, should be a dictionary
|
|
|
|
Returns:
|
|
Processed dictionary of hash fields and values
|
|
"""
|
|
if hash_data is None:
|
|
return {}
|
|
|
|
try:
|
|
if isinstance(hash_data, dict):
|
|
return {k: KeyService._parse_value(v) for k, v in hash_data.items()}
|
|
return hash_data
|
|
except Exception as e:
|
|
logger.error(f"Error parsing hash data: {e}")
|
|
return {} if hash_data is None else hash_data
|
|
|
|
@staticmethod
|
|
def _parse_value(value):
|
|
"""
|
|
Parse a value, handling special formats like arrays.
|
|
|
|
Args:
|
|
value: The value to parse
|
|
|
|
Returns:
|
|
Parsed value
|
|
"""
|
|
if not isinstance(value, str):
|
|
return value
|
|
|
|
if value.startswith('[') and value.endswith(']'):
|
|
try:
|
|
array_str = value[1:-1]
|
|
array_values = [v.strip() for v in array_str.split(',')]
|
|
return ", ".join(array_values)
|
|
except Exception as e:
|
|
logger.error(f"Error parsing array value: {e}")
|
|
return value
|
|
return value
|
|
|
|
@staticmethod
|
|
def clean_redis_value(value):
|
|
"""
|
|
Clean a Redis value by removing protocol prefixes.
|
|
|
|
Args:
|
|
value: The Redis value to clean
|
|
|
|
Returns:
|
|
Cleaned value
|
|
"""
|
|
if isinstance(value, str) and value.startswith(('+', '-', ':', '`', '*')):
|
|
return value[1:].strip()
|
|
return value
|
|
|
|
@staticmethod
|
|
def process_list_values(values):
|
|
"""
|
|
Process list values.
|
|
|
|
Args:
|
|
values: List of values to process
|
|
|
|
Returns:
|
|
Processed list of values
|
|
"""
|
|
if values is None:
|
|
return []
|
|
|
|
try:
|
|
return list(values) # Just ensure it's a list
|
|
except Exception as e:
|
|
logger.error(f"Error processing list values: {e}")
|
|
return values if isinstance(values, list) else [] |