File size: 7,966 Bytes
824bf31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
"""
Module: core.exceptions
Description: Custom exceptions for the application
Author: Anderson H. Silva
Date: 2025-01-24
License: Proprietary - All rights reserved
"""

from typing import Any, Dict, Optional


class CidadaoAIError(Exception):
    """Base exception for all Cidadão.AI errors."""
    
    def __init__(
        self,
        message: str,
        error_code: Optional[str] = None,
        details: Optional[Dict[str, Any]] = None
    ) -> None:
        """Initialize the exception."""
        super().__init__(message)
        self.message = message
        self.error_code = error_code or self.__class__.__name__
        self.details = details or {}
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert exception to dictionary."""
        return {
            "error": self.error_code,
            "message": self.message,
            "details": self.details
        }


# Agent exceptions
class AgentError(CidadaoAIError):
    """Base exception for agent-related errors."""
    pass


class AgentInitializationError(AgentError):
    """Raised when agent initialization fails."""
    pass


class AgentExecutionError(AgentError):
    """Raised when agent execution fails."""
    pass


class AgentCommunicationError(AgentError):
    """Raised when agents fail to communicate."""
    pass


class ReflectionError(AgentError):
    """Raised when agent reflection fails."""
    pass


class DataAnalysisError(AgentError):
    """Raised when data analysis fails."""
    pass


# Investigation exceptions
class InvestigationError(CidadaoAIError):
    """Base exception for investigation errors."""
    pass


class InvestigationNotFoundError(InvestigationError):
    """Raised when investigation is not found."""
    pass


class InvestigationTimeoutError(InvestigationError):
    """Raised when investigation times out."""
    pass


class InvestigationValidationError(InvestigationError):
    """Raised when investigation input is invalid."""
    pass


# Data source exceptions
class DataSourceError(CidadaoAIError):
    """Base exception for data source errors."""
    pass


class TransparencyAPIError(DataSourceError):
    """Raised when Portal Transparência API fails."""
    pass


class DataNotFoundError(DataSourceError):
    """Raised when requested data is not found."""
    pass


class DataValidationError(DataSourceError):
    """Raised when data validation fails."""
    pass


# LLM exceptions
class LLMError(CidadaoAIError):
    """Base exception for LLM-related errors."""
    pass


class LLMProviderError(LLMError):
    """Raised when LLM provider fails."""
    pass


class LLMTimeoutError(LLMError):
    """Raised when LLM request times out."""
    pass


class LLMRateLimitError(LLMError):
    """Raised when LLM rate limit is exceeded."""
    pass


class LLMResponseError(LLMError):
    """Raised when LLM response is invalid."""
    pass


# Memory exceptions
class MemoryError(CidadaoAIError):
    """Base exception for memory-related errors."""
    pass


class MemoryStorageError(MemoryError):
    """Raised when memory storage fails."""
    pass


class MemoryRetrievalError(MemoryError):
    """Raised when memory retrieval fails."""
    pass


class MemoryCorruptionError(MemoryError):
    """Raised when memory is corrupted."""
    pass


# Authentication exceptions
class AuthenticationError(CidadaoAIError):
    """Base exception for authentication errors."""
    pass


class InvalidCredentialsError(AuthenticationError):
    """Raised when credentials are invalid."""
    pass


class TokenExpiredError(AuthenticationError):
    """Raised when token has expired."""
    pass


class UnauthorizedError(AuthenticationError):
    """Raised when user is not authorized."""
    pass


class AccountLockedError(AuthenticationError):
    """Raised when account is locked."""
    pass


# API exceptions
class APIError(CidadaoAIError):
    """Base exception for API errors."""
    pass


class RateLimitError(APIError):
    """Raised when rate limit is exceeded."""
    pass


class ValidationError(APIError):
    """Raised when input validation fails."""
    pass


class ResourceNotFoundError(APIError):
    """Raised when resource is not found."""
    pass


class ConflictError(APIError):
    """Raised when there's a conflict."""
    pass


# Configuration exceptions
class ConfigurationError(CidadaoAIError):
    """Base exception for configuration errors."""
    pass


class MissingConfigurationError(ConfigurationError):
    """Raised when required configuration is missing."""
    pass


class InvalidConfigurationError(ConfigurationError):
    """Raised when configuration is invalid."""
    pass


# Database exceptions
class DatabaseError(CidadaoAIError):
    """Base exception for database errors."""
    pass


class ConnectionError(DatabaseError):
    """Raised when database connection fails."""
    pass


class QueryError(DatabaseError):
    """Raised when database query fails."""
    pass


class IntegrityError(DatabaseError):
    """Raised when database integrity is violated."""
    pass


# ML/Analysis exceptions
class AnalysisError(CidadaoAIError):
    """Base exception for analysis errors."""
    pass


class AnomalyDetectionError(AnalysisError):
    """Raised when anomaly detection fails."""
    pass


class InsufficientDataError(AnalysisError):
    """Raised when there's insufficient data for analysis."""
    pass


class ModelNotFoundError(AnalysisError):
    """Raised when ML model is not found."""
    pass


# Audit exceptions
class AuditError(CidadaoAIError):
    """Base exception for audit errors."""
    pass


class AuditLogError(AuditError):
    """Raised when audit logging fails."""
    pass


class AuditVerificationError(AuditError):
    """Raised when audit verification fails."""
    pass


# Ethics exceptions
class EthicsError(CidadaoAIError):
    """Base exception for ethics-related errors."""
    pass


class EthicsViolationError(EthicsError):
    """Raised when ethics guidelines are violated."""
    pass


class PrivacyViolationError(EthicsError):
    """Raised when privacy is violated."""
    pass


# Notification exceptions
class NotificationError(CidadaoAIError):
    """Base exception for notification errors."""
    pass


class EmailError(NotificationError):
    """Raised when email sending fails."""
    pass


class WebhookError(NotificationError):
    """Raised when webhook fails."""
    pass


# File handling exceptions
class FileError(CidadaoAIError):
    """Base exception for file-related errors."""
    pass


class FileSizeError(FileError):
    """Raised when file size exceeds limit."""
    pass


class FileTypeError(FileError):
    """Raised when file type is not allowed."""
    pass


class FileProcessingError(FileError):
    """Raised when file processing fails."""
    pass


# External service exceptions
class ExternalServiceError(CidadaoAIError):
    """Base exception for external service errors."""
    pass


class ServiceUnavailableError(ExternalServiceError):
    """Raised when external service is unavailable."""
    pass


class ServiceTimeoutError(ExternalServiceError):
    """Raised when external service times out."""
    pass


# Report generation exceptions
class ReportError(CidadaoAIError):
    """Base exception for report errors."""
    pass


class ReportGenerationError(ReportError):
    """Raised when report generation fails."""
    pass


class ReportTemplateError(ReportError):
    """Raised when report template is invalid."""
    pass


# Custom HTTP exception handlers
def create_error_response(
    error: CidadaoAIError,
    status_code: int = 500
) -> Dict[str, Any]:
    """
    Create a standardized error response.
    
    Args:
        error: The exception instance
        status_code: HTTP status code
        
    Returns:
        Error response dictionary
    """
    return {
        "status": "error",
        "status_code": status_code,
        "error": error.to_dict()
    }