Coverage for notion_client/errors.py: 100%
51 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-07-13 22:22 +0000
« prev ^ index » next coverage.py v7.6.0, created at 2024-07-13 22:22 +0000
1"""Custom exceptions for notion-sdk-py.
3This module defines the exceptions that can be raised when an error occurs.
4"""
5from enum import Enum
6from typing import Optional
8import httpx
11class RequestTimeoutError(Exception):
12 """Exception for requests that timeout.
14 The request that we made waits for a specified period of time or maximum number of
15 retries to get the response. But if no response comes within the limited time or
16 retries, then this Exception is raised.
17 """
19 code = "notionhq_client_request_timeout"
21 def __init__(self, message: str = "Request to Notion API has timed out") -> None:
22 super().__init__(message)
25class HTTPResponseError(Exception):
26 """Exception for HTTP errors.
28 Responses from the API use HTTP response codes that are used to indicate general
29 classes of success and error.
30 """
32 code: str = "notionhq_client_response_error"
33 status: int
34 headers: httpx.Headers
35 body: str
37 def __init__(self, response: httpx.Response, message: Optional[str] = None) -> None:
38 if message is None:
39 message = (
40 f"Request to Notion API failed with status: {response.status_code}"
41 )
42 super().__init__(message)
43 self.status = response.status_code
44 self.headers = response.headers
45 self.body = response.text
48class APIErrorCode(str, Enum):
49 Unauthorized = "unauthorized"
50 """The bearer token is not valid."""
52 RestrictedResource = "restricted_resource"
53 """Given the bearer token used, the client doesn't have permission to
54 perform this operation."""
56 ObjectNotFound = "object_not_found"
57 """Given the bearer token used, the resource does not exist.
58 This error can also indicate that the resource has not been shared with owner
59 of the bearer token."""
61 RateLimited = "rate_limited"
62 """This request exceeds the number of requests allowed. Slow down and try again."""
64 InvalidJSON = "invalid_json"
65 """The request body could not be decoded as JSON."""
67 InvalidRequestURL = "invalid_request_url"
68 """The request URL is not valid."""
70 InvalidRequest = "invalid_request"
71 """This request is not supported."""
73 ValidationError = "validation_error"
74 """The request body does not match the schema for the expected parameters."""
76 ConflictError = "conflict_error"
77 """The transaction could not be completed, potentially due to a data collision.
78 Make sure the parameters are up to date and try again."""
80 InternalServerError = "internal_server_error"
81 """An unexpected error occurred. Reach out to Notion support."""
83 ServiceUnavailable = "service_unavailable"
84 """Notion is unavailable. Try again later.
85 This can occur when the time to respond to a request takes longer than 60 seconds,
86 the maximum request timeout."""
89class APIResponseError(HTTPResponseError):
90 """An error raised by Notion API."""
92 code: APIErrorCode
94 def __init__(
95 self, response: httpx.Response, message: str, code: APIErrorCode
96 ) -> None:
97 super().__init__(response, message)
98 self.code = code
101def is_api_error_code(code: str) -> bool:
102 """Check if given code belongs to the list of valid API error codes."""
103 if isinstance(code, str):
104 return code in (error_code.value for error_code in APIErrorCode)
105 return False