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