
    KCjC                         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
NN
(gkk.>.>-?r#**NSYY'   OO..{{$
 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#sf}SXUs   
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%(ZZ\#**,q/rKHHU)CUUEooe%;<G,1eWBwi(wL
KK'(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
LL
!'++"2"2!32cU;   *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>   