File size: 3,137 Bytes
8a6f58d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
Custom Error Classes and Error Messages
Provides intelligent error handling with user-friendly messages
"""

from typing import Optional


class VoiceDetectionError(Exception):
    """Base exception for voice detection errors"""
    
    def __init__(self, message: str, error_code: str, details: Optional[str] = None):
        """
        Initialize error
        
        Args:
            message: User-friendly error message
            error_code: Machine-readable error code
            details: Optional additional details
        """
        self.message = message
        self.error_code = error_code
        self.details = details
        super().__init__(self.message)


# Error message templates
ERROR_MESSAGES = {
    "AUDIO_TOO_SHORT": {
        "message": "Audio must be at least 1 second long",
        "code": "AUDIO_TOO_SHORT"
    },
    "AUDIO_TOO_LONG": {
        "message": "Audio must be less than 60 seconds long",
        "code": "AUDIO_TOO_LONG"
    },
    "UNSUPPORTED_FORMAT": {
        "message": "Only MP3 format is supported",
        "code": "UNSUPPORTED_FORMAT"
    },
    "INVALID_BASE64": {
        "message": "Invalid Base64 encoding",
        "code": "INVALID_BASE64"
    },
    "DECODE_ERROR": {
        "message": "Failed to decode audio file",
        "code": "DECODE_ERROR"
    },
    "PROCESSING_ERROR": {
        "message": "Failed to process audio",
        "code": "PROCESSING_ERROR"
    },
    "FEATURE_EXTRACTION_ERROR": {
        "message": "Failed to extract audio features",
        "code": "FEATURE_EXTRACTION_ERROR"
    },
    "PREDICTION_ERROR": {
        "message": "Failed to make prediction",
        "code": "PREDICTION_ERROR"
    },
    "UNAUTHORIZED": {
        "message": "Invalid or missing authentication",
        "code": "UNAUTHORIZED"
    },
    "INVALID_CREDENTIALS": {
        "message": "Invalid email or password",
        "code": "INVALID_CREDENTIALS"
    },
    "USER_EXISTS": {
        "message": "User with this email already exists",
        "code": "USER_EXISTS"
    },
    "USER_NOT_FOUND": {
        "message": "User not found",
        "code": "USER_NOT_FOUND"
    },
    "INVALID_TOKEN": {
        "message": "Invalid or expired token",
        "code": "INVALID_TOKEN"
    },
    "WEAK_PASSWORD": {
        "message": "Password must be at least 8 characters long",
        "code": "WEAK_PASSWORD"
    },
    "INVALID_EMAIL": {
        "message": "Invalid email format",
        "code": "INVALID_EMAIL"
    }
}


def create_error_response(error_code: str, details: Optional[str] = None) -> dict:
    """
    Create a standardized error response
    
    Args:
        error_code: Error code from ERROR_MESSAGES
        details: Optional additional details
        
    Returns:
        dict: Structured error response
    """
    error_info = ERROR_MESSAGES.get(error_code, {
        "message": "An unexpected error occurred",
        "code": "UNKNOWN_ERROR"
    })
    
    response = {
        "error": error_info["code"],
        "message": error_info["message"]
    }
    
    if details:
        response["details"] = details
    
    return response