Skip to content

Commit

Permalink
feat: Introduce new exception hierarchy including BoxSDKError and Box…
Browse files Browse the repository at this point in the history
…APIError (#57)
  • Loading branch information
box-sdk-build committed Feb 14, 2024
1 parent 873af42 commit 4a42b1a
Show file tree
Hide file tree
Showing 79 changed files with 2,269 additions and 1,579 deletions.
2 changes: 1 addition & 1 deletion box_sdk_gen/__init__.py
@@ -1,6 +1,6 @@
from .base_object import *

from .box_response import *
from .errors import *

from .fetch import *

Expand Down
38 changes: 0 additions & 38 deletions box_sdk_gen/box_response.py

This file was deleted.

6 changes: 4 additions & 2 deletions box_sdk_gen/ccg_auth.py
Expand Up @@ -20,6 +20,8 @@

from box_sdk_gen.managers.authorization import AuthorizationManager

from box_sdk_gen.errors import BoxSDKError


class CCGConfig:
def __init__(
Expand Down Expand Up @@ -173,8 +175,8 @@ def downscope_token(
"""
token: Optional[AccessToken] = self.token_storage.get()
if token == None:
raise Exception(
'No access token is available. Make an API call to retrieve a token before calling this method.'
raise BoxSDKError(
message='No access token is available. Make an API call to retrieve a token before calling this method.'
)
auth_manager: AuthorizationManager = (
AuthorizationManager(network_session=network_session)
Expand Down
6 changes: 5 additions & 1 deletion box_sdk_gen/developer_token_auth.py
Expand Up @@ -6,6 +6,8 @@

from box_sdk_gen.network import NetworkSession

from box_sdk_gen.errors import BoxSDKError


class BoxDeveloperTokenAuth(Authentication):
def __init__(self, token: str, **kwargs):
Expand All @@ -30,4 +32,6 @@ def refresh_token(
:param network_session: An object to keep network session state
:type network_session: Optional[NetworkSession], optional
"""
raise Exception('Developer token has expired. Please provide a new one.')
raise BoxSDKError(
message='Developer token has expired. Please provide a new one.'
)
119 changes: 119 additions & 0 deletions box_sdk_gen/errors.py
@@ -0,0 +1,119 @@
import pprint
from typing import Optional

from typing import Dict


class BoxSDKError(Exception):
def __init__(
self,
message: str,
timestamp: str = None,
error: Optional[Exception] = None,
type: Optional[str] = None,
**kwargs,
):
super().__init__(message)
self.message = message
self.timestamp = timestamp
self.error = error
self.type = type

def __str__(self):
return ''.join(
(
f'\nTimestamp: {self.timestamp}',
f'\nUnderlying error: {self.error}',
f'\nMessage: {self.message}',
)
)


class RequestInfo:
def __init__(
self,
method: str,
url: str,
query_params: Dict[str, str],
headers: Dict[str, str],
body: Optional[str] = None,
):
self.method = method
self.url = url
self.query_params = query_params
self.headers = headers
self.body = body

def __str__(self):
return ''.join(
(
f'\n\tMethod: {self.method}',
f'\n\tURL: {self.url}',
f'\n\tQuery params: \n{pprint.pformat(self.query_params, indent=8)}',
f'\n\tHeaders: \n{pprint.pformat(self.headers, indent=8)}',
''.join(
[
'\n\tBody: ',
'\n' if self.body else '',
pprint.pformat(self.body, indent=8),
]
),
)
)


class ResponseInfo:
def __init__(
self,
status_code: int,
headers: Dict[str, str],
body: Dict = None,
raw_body: Optional[str] = None,
):
self.status_code = status_code
self.headers = headers
self.body = body
self.raw_body = raw_body

def __str__(self):
return ''.join(
(
f'\n\tStatus code: {self.status_code}',
f'\n\tHeaders: \n{pprint.pformat(self.headers, indent=8)}',
''.join(
[
'\n\tBody: ',
'\n' if self.body else '',
pprint.pformat(self.body, indent=8),
]
),
f'\n\tRaw body: {self.raw_body}',
)
)


class BoxAPIError(BoxSDKError):
def __init__(
self,
request_info: RequestInfo,
response_info: ResponseInfo,
message: str,
timestamp: str,
error: Optional[str] = None,
type: Optional[str] = None,
**kwargs,
):
super().__init__(
message=message, timestamp=timestamp, error=error, type=type, **kwargs
)
self.request_info = request_info
self.response_info = response_info

def __str__(self):
return ''.join(
[
f'\t{super(BoxAPIError, self).__str__()}',
f'\nRequest: {self.request_info}',
f'\nResponse: {self.response_info}',
]
)

0 comments on commit 4a42b1a

Please sign in to comment.