# Error handling utilities for dwpose-editor import gradio as gr import time import functools from .notifications import notify_error, notify_warning, notify_success class DWPoseEditorError(Exception): """アプリケーション固有のエラー基底クラス""" pass class ModelLoadError(DWPoseEditorError): """モデル読み込みエラー""" pass class PoseDetectionError(DWPoseEditorError): """ポーズ検出エラー""" pass class ImageProcessingError(DWPoseEditorError): """画像処理エラー""" pass def handle_error(error, context=""): """統一エラーハンドラ""" if isinstance(error, ModelLoadError): notify_error("モデルの読み込みに失敗しました。しばらく待ってから再試行してください。") return None elif isinstance(error, PoseDetectionError): notify_warning("ポーズを検出できませんでした。別の画像をお試しください。") return None elif isinstance(error, ImageProcessingError): notify_error("画像の処理中にエラーが発生しました。") return None else: notify_error(f"予期しないエラーが発生しました: {str(error)}") return None def with_retry(max_retries=3, delay=1.0): """リトライ機能付きデコレータ""" def decorator(func): @functools.wraps(func) def wrapper(*args, **kwargs): last_exception = None for attempt in range(max_retries): try: return func(*args, **kwargs) except Exception as e: last_exception = e if attempt < max_retries - 1: print(f"リトライ {attempt + 1}/{max_retries}: {str(e)}") time.sleep(delay) else: print(f"最大リトライ回数に達しました: {str(e)}") # 最後の例外を再発生 if last_exception: raise last_exception return None return wrapper return decorator def safe_execute(operation, error_message="操作中にエラーが発生しました", show_error=True): """安全な操作実行""" try: return operation() except Exception as e: print(f"Safe execute error: {str(e)}") if show_error: notify_error(error_message) return None