
    i               
       R   d Z ddlmZ ddlmZ ddlmZ ddlZddlm	Z	 ddl
mZmZmZmZmZmZmZmZmZ dd	d
d
ddddd
d	ZdededefdZdededefdZdededefdZdededefdZdededefdZdededefdZdededefdZdededefdZdededefdZdededefdZd Z y)z8Error handling utilities for the DevAll workflow system.    )Request)JSONResponse)jsonable_encoderN)get_server_logger)	MACExceptionValidationErrorSecurityErrorConfigurationErrorWorkflowExecutionErrorResourceNotFoundErrorResourceConflictErrorTimeoutErrorExternalServiceErrori  i    i  i  i  i  )	VALIDATION_ERRORSECURITY_ERRORCONFIGURATION_ERRORWORKFLOW_EXECUTION_ERRORRESOURCE_NOT_FOUNDRESOURCE_CONFLICTTIMEOUT_ERROREXTERNAL_SERVICE_ERRORGENERIC_ERRORrequestexcreturnc                 6   K   t        | |       d{   S 7 w)zHandle validation errors.Nhandle_mac_exceptionr   r   s     @/Users/bowang/.openclaw/workspace/ChatDev/utils/error_handler.pyhandle_validation_errorr"           %gs3333   c                 6   K   t        | |       d{   S 7 w)zHandle security errors.Nr   r    s     r!   handle_security_errorr&       r#   r$   c                 6   K   t        | |       d{   S 7 w)zHandle configuration errors.Nr   r    s     r!   handle_configuration_errorr(   %   r#   r$   c                 6   K   t        | |       d{   S 7 w)z!Handle workflow execution errors.Nr   r    s     r!   handle_workflow_execution_errorr*   *   r#   r$   c                 6   K   t        | |       d{   S 7 w)z!Handle resource not found errors.Nr   r    s     r!   handle_resource_not_found_errorr,   /   r#   r$   c                 6   K   t        | |       d{   S 7 w)z Handle resource conflict errors.Nr   r    s     r!   handle_resource_conflict_errorr.   4   r#   r$   c                 6   K   t        | |       d{   S 7 w)zHandle timeout errors.Nr   r    s     r!   handle_timeout_errorr0   9   r#   r$   c                 6   K   t        | |       d{   S 7 w)zHandle external service errors.Nr   r    s     r!   handle_external_service_errorr2   >   r#   r$   c           	      .  K   t               }|j                  |d|j                   d|j                   t	        | j
                  dd      t        | j                        | j                         t        j                  |j                  d      }|j                  |j                  |j                  d|j                  j                  dt        d	      j                  j                         j!                               d
}t#        |t%        |            S w)z@Handle DevAll exceptions and return standardized error response.zDevAll exception occurred:  - correlation_idNr5   urlmethodr   codemessagedetails
_timestampdatetime)error	timestampstatus_codecontent)r   log_exception
error_coder;   getattrstatestrr7   r8   ERROR_CODE_TO_STATUSgetr<   __dict__
__import__r>   utcnow	isoformatr   r   )r   r   loggerrB   response_datas        r!   r   r   C   s      F 
%cnn%5SFw}}.>E~~   '**3>>3?K
 NN{{{{

 \\%%lJz4J4S4S4Z4Z4\4f4f4hiM  / s   DDc           	        K   t               }|j                  |dt        |      j                   dt	        |       t        | j                  dd      t	        | j                        | j                         ddi d}d	dl	}|j                  d
      dk(  r:t        |      j                  t	        |      t        j                         d|d   d<   t        dt        d|i            S w)zAHandle general exceptions and return standardized error response.zGeneral exception occurred: r4   r5   Nr6   INTERNAL_ERRORz!An internal server error occurredr9   r   ENVIRONMENTdevelopment)exception_typeexception_message	tracebackr<   
debug_infor   r?   rA   )r   rD   type__name__rH   rF   rG   r7   r8   osgetenvrW   
format_excr   r   )r   r   rO   error_detailsr[   s        r!   handle_general_exceptionr_   c   s      F 
&tCy'9'9&:#c#hZHw}}.>E~~   !6M 	yy=0"3i00!$S"--/2
i .  '=!9: s   C%C'c                    | j                  t        t               | j                  t        t               | j                  t
        t               | j                  t        t               | j                  t        t               | j                  t        t               | j                  t        t               | j                  t        t                | j                  t"        t$               | j                  t&        t(               | S )z&Add exception handlers to FastAPI app.)add_exception_handlerr   r"   r	   r&   r
   r(   r   r*   r   r,   r   r.   r   r0   r   r2   r   r   	Exceptionr_   )apps    r!   add_exception_handlersrd      s    o/FGm-BC02LM46UV35TU35STl,@A24QRl,@Ai)ABJ    )!__doc__fastapir   fastapi.responsesr   fastapi.encodersr   rW   utils.structured_loggerr   utils.exceptionsr   r   r	   r
   r   r   r   r   r   rI   r"   r&   r(   r*   r,   r.   r0   r2   r   rb   r_   rd    re   r!   <module>rm      sc   >  * -  5m m m  #!
 47 4 4\ 4
4 4} 4 4
4g 4<N 4S_ 4
47 4AW 4\h 4
47 4AV 4[g 4
4' 4@U 4Zf 4
4 4l 4| 4
4 4?S 4Xd 4
 l | @ G  )    Fre   