Ë
    KµCjC  ã                   ó”   — d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZ  e
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y)ua  
Global exception handlers.

Registered on the FastAPI app in main.py. These guarantee that NOTHING
ever reaches the client as a raw stack trace or an unstructured error â€”
every error response has the same shape (ErrorResponse), and every
unexpected exception is logged with full detail server-side while the
client sees only a safe, generic message.
é    )ÚRequest)ÚRequestValidationError)ÚJSONResponse)ÚAppException)Ú
get_loggerÚrequest_id_ctxzapp.exceptionsÚrequestÚexcÚreturnc              ƒ   ó8  K  — t        j                  «       }t        j                  d|j                  › d| j
                  j                  › d|j                  › �d|j                  i¬«       t        |j                  |j                  |j                  |dœ¬«      S ­w)NzAppException [z] on ú: Ú
extra_data)Úextra©Ú
error_codeÚmessageÚ
request_id©Ústatus_codeÚcontent)r   ÚgetÚloggerÚwarningr   ÚurlÚpathÚdetailr   r   r   r   ©r	   r
   r   s      úNC:\Crop_Prediction\Backend\crop-ai-system\app\middleware\exception_handlers.pyÚapp_exception_handlerr      s‡   è ø€ Ü×#Ñ#Ó%€JÜ
‡N�NØ
˜Ÿ™Ð(¨¨g¯k©k×.>Ñ.>Ð-?¸rÀ#Ç*Á*ÀÐNØ˜SŸY™YÐ'ð ô ô Ø—O‘OàŸ.™.Ø—{‘{Ø$ñ
ôð ùs   ‚BBc              ƒ   ó†  K  — t        j                  «       }|j                  «       r|j                  «       d   ni }dj                  d„ |j                  dg «      D «       «      }|j                  dd«      }|r|› d|› �n|}t        j                  d| j                  j                  › d|› �«       t        d	d
||dœ¬«      S ­w)a  
    Pydantic validation errors from request body parsing. We reshape
    FastAPI's default (verbose, implementation-detail-heavy) error format
    into the same ErrorResponse contract as every other error, so the
    client's error handling code only needs one code path.
    r   ú.c              3   ó>   K  — | ]  }|d k7  sŒ	t        |«      –— Œ y­w)ÚbodyN)Ústr)Ú.0Úlocs     r   ú	<genexpr>z/validation_exception_handler.<locals>.<genexpr>.   s   è ø€ ÒU #ÀsÈfÃ}”S˜—XÑUùs   ‚
�r&   ÚmsgzInvalid request datar   zValidation failed on i¦  ÚVALIDATION_FAILEDr   r   )	r   r   ÚerrorsÚjoinr   Úinfor   r   r   )r	   r
   r   Úfirst_errorÚfieldr   Úfull_messages          r   Úvalidation_exception_handlerr0   %   s¶   è ø€ ô  ×#Ñ#Ó%€JØ%(§Z¡Z¤\�#—*‘*“,˜q’/°r€KØ�H‰HÑU¨¯©¸ÀÓ)CÔUÓU€EØ�o‰o˜eÐ%;Ó<€GÙ,1�e�W˜B˜w˜iÑ(°w€Lä
‡K�KÐ'¨¯©×(8Ñ(8Ð'9¸¸L¸>ÐJÔKäØà-Ø#Ø$ñ
ôð ùs   ‚B?Cc              ƒ   ó¶   K  — t        j                  «       }t        j                  d| j                  j
                  › d|› �d¬«       t        ddd|dœ¬	«      S ­w)
zÛ
    The last line of defense. If we reach here, something we did not
    anticipate broke. Full traceback goes to logs/Sentry; the client gets
    a generic, safe message plus the request_id so they can report it.
    zUnhandled exception on r   T)Úexc_infoiô  ÚINTERNAL_ERRORz\An unexpected error occurred. Please try again or contact support with the request id below.r   r   )r   r   r   Úerrorr   r   r   r   s      r   Úunhandled_exception_handlerr5   >   sg   è ø€ ô  ×#Ñ#Ó%€JÜ
‡L�LØ
! '§+¡+×"2Ñ"2Ð!3°2°c°UÐ;Øð ô ô Øà*ð4à$ñ	
ôð ùs   ‚AAN)Ú__doc__Úfastapir   Úfastapi.exceptionsr   Úfastapi.responsesr   Úapp.core.exceptionsr   Úapp.core.logging_configr   r   r   r   r0   Ú	Exceptionr5   © ó    r   ú<module>r?      sv   ðñõ Ý 5Ý *å ,ß >á	Ð$Ó	%€ð¨ð °|ð Èó ð °ð Ð>Tð ÐYeó ð2¨wð ¸Yð È<ô r>   