
    Sjg                         d Z ddlZddlmZmZmZ  G d de      Z G d de      Z G d d	e      Z	 G d
 de      Z
 G d de      Z G d de      Z G d de      Z G d de      Zdej                  deddfdZy)z&
Error handling utilities for v2 API.
    N)DictAnyOptionalc                   R     e Zd ZdZddedee   deej                     f fdZ	 xZ
S )FirecrawlErrorz(Base exception for Firecrawl API errors.messagestatus_coderesponsec                 @    t         |   |       || _        || _        y )N)super__init__r	   r
   )selfr   r	   r
   	__class__s       S/root/.hermes/venv/lib/python3.12/site-packages/firecrawl/v2/utils/error_handler.pyr   zFirecrawlError.__init__   s    !&     )NN)__name__
__module____qualname____doc__strr   intrequestsResponser   __classcell__)r   s   @r   r   r   	   s6    2! !(3- !RZ[c[l[lRm ! !r   r   c                       e Zd ZdZy)BadRequestErrorz)Raised when the request is invalid (400).Nr   r   r   r    r   r   r   r      s    3r   r   c                       e Zd ZdZy)UnauthorizedErrorz.Raised when the request is unauthorized (401).Nr   r   r   r   r    r       s    8r   r    c                       e Zd ZdZy)PaymentRequiredErrorz&Raised when payment is required (402).Nr   r   r   r   r"   r"      s    0r   r"   c                       e Zd ZdZy)WebsiteNotSupportedErrorz+Raised when website is not supported (403).Nr   r   r   r   r$   r$   "   s    5r   r$   c                       e Zd ZdZy)RequestTimeoutErrorz$Raised when request times out (408).Nr   r   r   r   r&   r&   '   s    .r   r&   c                       e Zd ZdZy)RateLimitErrorz-Raised when the rate limit is exceeded (429).Nr   r   r   r   r(   r(   ,   s    7r   r(   c                       e Zd ZdZy)InternalServerErrorz3Raised when there's an internal server error (500).Nr   r   r   r   r*   r*   1   s    =r   r*   r
   actionreturnc                 F   	 | j                         }|j                  dd      }|j                  dd      }| j                  dk(  r"d| d| d| }t        || j                  |       | j                  dk(  r"d| d| d| }t        || j                  |       | j                  dk(  r"d| d| d| }t        || j                  |       | j                  dk(  r"d| d| d| }t        || j                  |       | j                  dk(  r"d| d| d| }t        || j                  |       | j                  dk(  r"d| d| d| }t        || j                  |       | j                  dk(  r"d| d| d| }t        || j                  |       d| d| j                   d| d| }t        || j                  |       #  	 | j                  dd }|j                         rd| }d| j                   }nd	| j                   }d
}n#  d| j                   }d
}Y nxY wY xY w)a  
    Handle API response errors and raise appropriate exceptions.
    
    Args:
        response: The HTTP response object
        action: Description of the action being performed
        
    Raises:
        FirecrawlError: Appropriate error based on status code
    errorzNo error message provided.detailsz%No additional error details provided.Ni  z#Server returned non-JSON response: zFull response status: z+Server returned empty response with status zNo additional details availablez0Server returned unreadable response with status i  zBad Request: Failed to z. z - i  zUnauthorized: Failed to i  zPayment Required: Failed to i  z!Website Not Supported: Failed to i  zRequest Timeout: Failed to z as the request timed out. i  zRate Limit Exceeded: Failed to z!Internal Server Error: Failed to zUnexpected error during z: Status code )jsongettextstripr	   r   r    r"   r$   r&   r(   r*   r   )r
   r+   response_jsonerror_messageerror_detailsresponse_textr   s          r   handle_response_errorr8   6   s   > %))'3OP%)))5\]  s"+F82m_CWgx';';XFF				$,VHB}oSX)=)=xHH				$0=/]O\"7H,@,@(KK				$5fXRcR_Q`a&w0D0DhOO				$/x7RS`Raaderdst!'8+?+?JJ				$3F82m_CP]_Wh&:&:HEE				$5fXRcR_Q`a!'8+?+?JJ,VHN8CWCWBXXZ[hZiilmzl{|Wh&:&:HEEM>
	>$MM$3/M""$"Em_ U"89M9M8N O"MhNbNbMc d A	>NxOcOcNdeM=Ms$   4F< <H ?AHH HH )r   r   typingr   r   r   	Exceptionr   r   r    r"   r$   r&   r(   r*   r   r   r8   r   r   r   <module>r;      s     & &!Y !	n 		 	
	> 	
	~ 	
	. 	
	^ 	
	. 	
5FH$5$5 5Fs 5Ft 5Fr   