File size: 2,517 Bytes
4555cad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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