
    =Ki2                        S r SSKJr  SSKJrJr  SSKrSSKJrJ	r	J
r
JrJrJrJrJrJrJrJrJr  SS jrSS jrSS jrSS	 jrSS
 jrSS jrSS jrSS jrSS jrSS jrg)zShared helper functions for error handling.

These functions are used by both sync and async clients to parse error responses
and raise appropriate exceptions. They contain no I/O operations.
    )annotations)AnyOptionalN)QuotaExceededErrorResourceAlreadyExistsErrorResourceNotFoundErrorResourceTimeoutErrorSandboxAPIErrorSandboxAuthenticationErrorSandboxClientErrorSandboxConnectionErrorSandboxCreationErrorSandboxNotReadyErrorSandboxOperationErrorValidationErrorc           	     ^    U R                   R                  5       nUR                  S5      n[        U[        5      (       a-  UR                  S5      UR                  S[        U 5      5      S.$ [        U[        5      (       aq  U(       aj  U Vs/ s H5  n[        U[        5      (       d  M  UR                  S[        U5      5      PM7     nnSU(       a  SR                  U5      S.$ [        U 5      S.$ SU=(       d    [        U 5      S.$ s  snf ! [         a    S[        U 5      S.s $ f = f)zParse standardized error response.

Expected format: {"detail": {"error": "...", "message": "..."}}

Returns a dict with:
- error_type: The error type (e.g., "ImagePull", "CrashLoop")
- message: Human-readable error message
detailerrormessage
error_typer   msgNz; )	responsejsonget
isinstancedictstrlistjoin	Exception)r   datar   dmessagess        \/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/langsmith/sandbox/_helpers.pyparse_error_responser&   !   s   ;~~""$(# fd##$jj1!::iU<  fd##6<Tf
1d@S,eSV,fHT"2:499X. @CE
  #v/CUDD U  ;"s5z::;s<   A,D / D D,DD (D 5D D D,+D,c                H    U R                   R                  5       nUR                  S5      n[        U[        5      (       a-  UR                  S5      UR                  S[        U 5      5      S.$ SU=(       d    [        U 5      S.$ ! [         a    S[        U 5      S.s $ f = f)zParse error response (simplified version for sandbox operations).

Returns a dict with:
- error_type: The error type
- message: Human-readable error message
r   r   r   r   N)r   r   r   r   r   r   r!   r   r"   r   s      r%   parse_error_response_simpler)   C   s    ;~~""$(#fd##$jj1!::iU< 
 #v/CUDD ;"s5z::;s   A,B /B B! B!c                     U R                   R                  5       nUR                  S/ 5      n[        U[        5      (       a  U$ / $ ! [
         a    / s $ f = f)zParse Pydantic validation error response.

Returns a list of validation error details, each containing:
- loc: Location of the error (e.g., ["body", "resources", "cpu"])
- msg: Human-readable error message
- type: Error type (e.g., "value_error")
r   )r   r   r   r   r   r!   r(   s      r%   parse_validation_errorr+   Y   sT    ~~""$(B'fd##M	 	s   AA A AAc                    U R                  5       nSU;   a  SU;   d  SU;   a  gSU;   a  gSU;   a  gSU;   a  SU;   d  SU;   a  gS	U;   a  g	g
)z~Extract quota type from error message.

Returns one of: "sandbox_count", "cpu", "memory", "volume_count",
"storage", or None.
sandboxcountlimitsandbox_countcpumemoryvolumevolume_countstorageN)lower)r   message_lowers     r%   extract_quota_typer8   k   sg     MMOMM!= G}$<	-		]	"	]	"= G}$<	m	#    c                V    [        U R                  SS5      U R                  S5      S9Ue)a  Raise SandboxCreationError with the error_type from the API response.

The error_type indicates the specific failure reason:
- ImagePull: Image pull failed
- CrashLoop: Container crashed during startup
- SandboxConfig: Configuration error
- Unschedulable: Cannot be scheduled
r   zSandbox creation failedr   r   )r   r   )r"   r   s     r%   raise_creation_errorr<      s1     5688L) r9   c                   U R                   R                  n[        U 5      nUS:X  a  [        US   SS9U eUS:X  af  [	        U 5      nU(       aH  [        S U 5       5      (       a1  U(       a  US   R                  SS	/5      S
   OS	n[        US   UUS9U e[        X 5        g	US:X  a  [        US   5      n[        US   US9U eUS:X  a&  [        US   UR                  S5      =(       d    SS9U e[        U 5        g	)a  Handle HTTP errors specific to sandbox creation.

Maps API error responses to specific exception types:
- 408: ResourceTimeoutError (sandbox didn't become ready in time)
- 422: ValidationError (bad input) or SandboxCreationError (runtime)
- 429: QuotaExceededError (org limits exceeded)
- 503: SandboxCreationError (no resources available)
- Other: Falls through to generic error handling
i  r   r-   resource_type  c              3  H   #    U  H  oR                  S 5      S:H  v   M     g7f)typevalue_errorN)r   ).0r#   s     r%   	<genexpr>0handle_sandbox_creation_error.<locals>.<genexpr>   s     K7a55=M97s    "r   locNr   fielddetails  r   
quota_type  r   Unschedulabler;   )r   status_coder&   r	   r+   anyr   r   r<   r8   r   r   handle_client_http_error)r   statusr"   rK   rJ   rN   s         r%   handle_sandbox_creation_errorrU      s    ^^''F&D}"4	?)LRWW	3(/sK7KKK9@GAJNN54&1"5dE!Y 	 !-	3'Y8
 O!
 	 
3"Oxx-@
 	 	!'r9   c                    U R                   R                  n[        U 5      nUS:X  a  [        US   SS9U eUS:X  a  [	        US   SS9U e[        U 5        g)	a  Handle HTTP errors specific to volume creation.

Maps API error responses to specific exception types:
- 503: SandboxCreationError (provisioning failed)
- 504: ResourceTimeoutError (volume didn't become ready in time)
- Other: Falls through to generic error handling
rO   r   VolumeProvisioningr;     r3   r>   N)r   rQ   r&   r   r	   rS   )r   rT   r"   s      r%   handle_volume_creation_errorrY      se     ^^''F&D}"O(<
	 
3"4	?(KQVV 	!'r9   c                   U R                   R                  n[        U 5      nUR                  S5      nUS:X  a4  US:X  a  [	        US   SS9U eUS:X  a  [        US   US9U e[        U 5        gUS	:X  a  [        US   S
S9U eUS:X  a  [        US   5      n[        US   US9U eUS:X  a  [        US   S
S9U e[        U 5        g)aw  Handle HTTP errors specific to pool creation/update.

Maps API error responses to specific exception types:
- 400: ResourceNotFoundError or ValidationError (template has volumes)
- 409: ResourceAlreadyExistsError
- 429: QuotaExceededError (org limits exceeded)
- 504: ResourceTimeoutError (timeout waiting for ready replicas)
- Other: Falls through to generic error handling
r     TemplateNotFoundr   templater>   r   r;   i  poolrL   rM   rX   N)r   rQ   r&   r   r   r   rS   r   r8   r   r	   )r   rT   r"   r   rN   s        r%   handle_pool_errorr_      s     ^^''F&D,'J}++'Yz ,,!$y/jIuT %U+	3(O6
	 
3'Y8
 O!
 	 
3"4	?&IuT 	!'r9   c                   [        U 5      nUS   nUR                  S5      nU R                  R                  nUS;   a  [	        U5      U eUS:X  a  [        U5      U eUS:X  a9  [        U 5      nU(       a  US   R                  SS/5      S	   OSn[        UUUS
9U eUS:X  a  [        U5      n[        UUS9U eUS:X  a  US:X  a  [        U5      U eUS:X  a  [        U5      U e[        U5      U e)zLHandle HTTP errors and raise appropriate exceptions (for client operations).r   r   )i      r@   r   rG   NrH   rI   rL   rM     ConnectionErrori  )r&   r   r   rQ   r   r   r+   r   r8   r   r   r
   r   )r   r"   r   r   rT   rK   rJ   rN   s           r%   rS   rS     s   &D9oG,'J^^''F(1u<}#G,%7 }(/5<
utf-b1$
 		 }'0
 !
 	
 }'88$W-58}g&E1
W
%50r9   c                   [        U 5      nUS   nUR                  S5      nU R                  R                  nUS:X  a  [	        USUS9U eUS:X  a  [	        USUS9U eUS:X  a  [	        US	US9U eUS
:X  a  [	        USSS9U eUS:X  a  US:X  a  [        U5      U eUS:X  a  US:X  a  [        U5      U eUS:X  d  US:X  a  [        USS9U e[        U5      U e)a  Handle HTTP errors for sandbox operations (run, read, write).

Maps API error types to specific exceptions:
- WriteError -> SandboxOperationError (operation="write")
- ReadError -> SandboxOperationError (operation="read")
- CommandError -> SandboxOperationError (operation="command")
- ConnectionError (502) -> SandboxConnectionError
- FileNotFound / 404 -> ResourceNotFoundError (resource_type="file")
- NotReady (400) -> SandboxNotReadyError
- 403 -> SandboxOperationError (permission denied)
r   r   
WriteErrorwrite)	operationr   	ReadErrorreadCommandErrorcommandra   NPermissionDeniedrc   rd   r[   NotReadyrb   FileNotFoundfiler>   )	r)   r   r   rQ   r   r   r   r   r   )r   r"   r   r   rT   s        r%   handle_sandbox_http_errorrq   9  s    'u-D9oG,'J^^''F \!#w:
	 [ #v*
	 ^##yZ
	
 }#t0B
	
 }'88$W-58 }z1"7+6}
n4#G6BM
W
%50r9   )r   httpx.HTTPStatusErrorreturndict[str, Any])r   rr   rs   z
list[dict])r   r   rs   zOptional[str])r"   rt   r   rr   rs   None)r   rr   rs   ru   )__doc__
__future__r   typingr   r   httpxlangsmith.sandbox._exceptionsr   r   r   r	   r
   r   r   r   r   r   r   r   r&   r)   r+   r8   r<   rU   rY   r_   rS   rq    r9   r%   <module>r|      s_    #      (;D;,$>-(`(0+(\"1T/1r9   