+
    5)i	                         R t ^ RIt^ RIt^ RIHtHtHtHt ^ RIH	t	 ^ RI
Ht ^ RIHt ^ RIHt R R ltR	 R
 ltR R ltR# )z{
Global Flask error handling middleware
Provides centralized error handling for the Flask API with Laravel-style responses
N)FlaskrequestjsonifygHTTPExceptionValidationException)ApiResponse)
get_loggerc                $    V ^8  d   QhR\         /#    appr   )formats   "ID:\Projects\python\client-ideal-oms_pyapi\app\middleware\error_handler.py__annotate__r      s     UW UW UW    c                V  a a \        R4      oS P                  \        4      R V3R ll4       pS P                  R4      V3R l4       pS P                  R4      V3R l4       pS P                  R4      V3R	 l4       pS P                  R
4      V3R l4       pS P                  R4      V3R l4       pS P                  R4      V3R l4       pS P                  R4      V V3R l4       pS P                  \        4      R V3R ll4       p	S P                  \        4      R V V3R ll4       p
R# )ze
Register global error handlers for the Flask application

Args:
    app: Flask application instance
error_handlerc                $    V ^8  d   QhR\         /# r   er   )r   s   "r   r   -register_error_handlers.<locals>.__annotate__   s     A A': Ar   c                   < SP                  RV P                   RV P                   24       \        P                  ! V P                  V P                  4      # )z<Handle ValidationException and return Laravel-style responsezValidation error: z - Errors: )warningmessageerrorsr
   validation_errorr   loggers   &r   handle_validation_exception<register_error_handlers.<locals>.handle_validation_exception   sB     	+AII;k!((LM++AHHaii@@r     c                n   < SP                  R\        V 4       24       \        P                  ! RRR7      # )zHandle 400 Bad Request errorszBad request: z.Bad request. Please check your request format.r$   status_coder   strr
   errorr    s   &r   handle_bad_request3register_error_handlers.<locals>.handle_bad_request   s1     	s1vh/0  !Q_bccr     c                n   < SP                  R\        V 4       24       \        P                  ! RRR7      # )zHandle 401 Unauthorized errorszUnauthorized access attempt: z>Unauthorized. Please provide valid authentication credentials.r-   r&   r(   r    s   &r   handle_unauthorized4register_error_handlers.<locals>.handle_unauthorized$   s2     	6s1vh?@  !aorssr     c                n   < SP                  R\        V 4       24       \        P                  ! RRR7      # )zHandle 403 Forbidden errorszForbidden access attempt: z=Forbidden. You don't have permission to access this resource.r1   r&   r(   r    s   &r   handle_forbidden1register_error_handlers.<locals>.handle_forbidden*   s2     	3CF8<=  !`nqrrr     c                x   < SP                  R\        P                   24       \        P                  ! RRR7      # )zHandle 404 Not Found errorszResource not found: z%The requested resource was not found.r5   r&   )r   r   urlr
   r*   r    s   &r   handle_not_found1register_error_handlers.<locals>.handle_not_found0   s2     	-gkk];<  !HVYZZr     c                   < SP                  R\        P                   R\        P                   24       \        P
                  ! RRR7      # )z$Handle 405 Method Not Allowed errorszMethod not allowed:  z%Method not allowed for this endpoint.r:   r&   )r   r   methodr7   r
   r*   r    s   &r   handle_method_not_allowed:register_error_handlers.<locals>.handle_method_not_allowed6   s=     	-gnn-=Qw{{mLM  !HVYZZr     c                x   < SP                  R\        P                   24       \        P                  ! RRR7      # )z#Handle 429 Too Many Requests errorszRate limit exceeded: z*Too many requests. Please try again later.r@   r&   )r   r   remote_addrr
   r*   r    s   &r   handle_too_many_requests9register_error_handlers.<locals>.handle_too_many_requests<   s5     	.w/B/B.CDE  !M[^__r     c                l  < SP                  R\        V 4       24       SP                  \        P                  ! 4       4       SP                  P                  R4      '       d;   \        P                   ! R\        V 4       R\        P                  ! 4        2RR7      # \        P                   ! RRR7      # )z5Handle 500 Internal Server Error - Always return JSONzInternal server error: DEBUG

Traceback:
rE   r&   z"An internal server error occurred.r*   r)   	traceback
format_excconfiggetr
   r   r   r!   s   &r   handle_internal_server_error=register_error_handlers.<locals>.handle_internal_server_errorB   s     	.s1vh78Y))+, ::>>'""$$)#a&1A)BVBVBXAYZ 
 $$%IWZ[[r   c                $    V ^8  d   QhR\         /# r   r   )r   s   "r   r   r   R   s     \ \ \r   c                   < SP                  RV P                   RV P                   24       \        P                  ! V P                  ;'       g    RV P                  R7      # )z3Handle generic HTTP exceptions - Always return JSONzHTTP exception: z - zAn error occurred.r&   )r   codedescriptionr
   r*   r    s   &r   handle_http_exception6register_error_handlers.<locals>.handle_http_exceptionQ   sN     	)!&&Q]]ODE  !F!F2FTUTZTZ[[r   c                $    V ^8  d   QhR\         /# r   )	Exception)r   s   "r   r   r   X   s     W WI Wr   c                l  < SP                  R\        V 4       24       SP                  \        P                  ! 4       4       SP                  P                  R4      '       d;   \        P                   ! R\        V 4       R\        P                  ! 4        2RR7      # \        P                   ! RRR7      # )z4Handle any unhandled exceptions - Always return JSONzUnhandled exception: rG   zUnhandled error: rH   rE   r&   zAn unexpected error occurred.rI   rN   s   &r   handle_generic_exception9register_error_handlers.<locals>.handle_generic_exceptionW   s     	,SVH56Y))+, ::>>'""$$#CF8+;I<P<P<R;ST 
 $$%DRUVVr   N)r   errorhandlerr	   r   rX   )r   r"   r+   r/   r3   r8   r>   rC   rO   rU   rZ   r!   s   f          @r   register_error_handlersr]      s^    (F)*A +A
 	cd d
 	ct t
 	cs s
 	c[ [
 	c[ [
 	c` `
 	c\ \ 	m$\ %\
 	i W W !Wr   c                $    V ^8  d   QhR\         /# r   r   )r   s   "r   r   r   g   s     ; ;5 ;r   c                   a a \        R4      oS P                  V V3R l4       pS P                  V V3R l4       pS P                  V3R l4       pR# )zj
Register request lifecycle handlers for the Flask application

Args:
    app: Flask application instance
request_handlerc                   < S P                   P                  R4      '       d   SP                  R\        P                   R\        P
                   24       \        P                  '       dC   \        P                  ! 4       '       d(   SP                  R\        P                  ! 4        24       \        P                  '       d.   SP                  R\        \        P                  4       24       R# R# R# )z;Run before each request for additional validation and setuprG   z	Request: r<   zJSON Body: zQuery Params: N)
rL   rM   debugr   r=   r7   is_jsonget_jsonargsdict)r   r!   s   r   before_request_validation<register_request_handlers.<locals>.before_request_validationp   s     ::>>'""LL9W^^$4Agkk]CD7#3#3#5#5{7+;+;+=*>?@|||~d7<<.@-ABC 	 #r   c                  < RV P                   R&   RV P                   R&   RV P                   R&   SP                  P                  RR4      '       d.   R	V P                   R
&   RV P                   R&   RV P                   R&   V P                  R8  d   V P                  '       g   V P
                  P                  R4      '       g~    RRRV P                  4       '       d   V P                  RR7      MRRV P                  /p\        P                  ! V4      V n
        RV n        SP                  RV P                   24       V # V #   \         d    pSP                  RT 24        Rp?T # Rp?ii ; i)z'Ensure all responses are JSON formattednosniffzX-Content-Type-OptionsDENYzX-Frame-Optionsz1; mode=blockzX-XSS-ProtectionENABLE_CORST*zAccess-Control-Allow-OriginzGET, POST, PUT, DELETE, OPTIONSzAccess-Control-Allow-MethodszContent-Type, AuthorizationzAccess-Control-Allow-Headersr$   zapplication/jsonr*   zAn error occurredr   )as_textzUnknown errorr'   z+Converted non-JSON error response to JSON: z$Failed to convert response to JSON: N)headersrL   rM   r'   rc   content_type
startswithget_datajsondumpsdatar   rX   r*   )response
error_datar   r   r!   s   &  r   ensure_json_response7register_request_handlers.<locals>.ensure_json_response{   sj    6?12.4*+/>+, ::>>-..>AH:;?`H;<?\H;< 3&###H,A,A,L,LM_,`,`M  !4!hFWFWFYFY8#4#4T#4#B_n%x';';"J
 %)JJz$:HM,>H)NN%PQYQeQePf#gh x ! MLL#Gs!KLLMs   E A!E E+E&&E+c                   < SP                  RV P                   R\        P                   R\        P                   24       V # )z"Run after each request for cleanupz
Response: z for r<   )rb   r'   r   r=   r7   )rv   r!   s   &r   after_request_cleanup8register_request_handlers.<locals>.after_request_cleanup   s9     	z("6"6!7uW^^<LAgkk][\r   N)r   before_requestafter_request)r   rg   rx   r{   r!   s   f   @r   register_request_handlersr   g   s_     )*FD D 	 @ 	 r   c                $    V ^8  d   QhR\         /# r   r   )r   s   "r   r   r      s     # #e #r   c                2    \        V 4       \        V 4       R# )z`
Complete error handling setup for Flask application

Args:
    app: Flask application instance
N)r]   r   )r   s   &r   setup_error_handlingr      s     C c"r   )__doc__rJ   rs   flaskr   r   r   r   werkzeug.exceptionsr   #app.exceptions.validation_exceptionr	   app.utils.response_helperr
   app.utils.helpersr   r]   r   r    r   r   <module>r      s7   
   , , - C 1 (UWp;|#r   