Skip to content

Commit 91f5e1a

Browse files
authored
Doug/fix api error (#29)
* Fixed error handling for raising exceptions * Update version for deploy
1 parent 231ee35 commit 91f5e1a

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

socketdev/__init__.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import logging
2-
31
from socketdev.core.api import API
42
from socketdev.dependencies import Dependencies
53
from socketdev.export import Export
@@ -18,6 +16,7 @@
1816
from socketdev.triage import Triage
1917
from socketdev.utils import Utils, IntegrationType, INTEGRATION_TYPES
2018
from socketdev.version import __version__
19+
from socketdev.log import log
2120

2221

2322
__author__ = "socket.dev"
@@ -30,8 +29,7 @@
3029

3130
api_url = "https://api.socket.dev/v0"
3231
request_timeout = 1200
33-
log = logging.getLogger("socketdev")
34-
log.addHandler(logging.NullHandler())
32+
3533

3634
# TODO: Add debug flag to constructor to enable verbose error logging for API response parsing.
3735

socketdev/core/api.py

+24-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import base64
2+
from socketdev.log import log
3+
24
import requests
35
from socketdev.core.classes import Response
46
from socketdev.exceptions import (
@@ -68,17 +70,21 @@ def format_headers(headers_dict):
6870
try:
6971
error_message = response.json().get("error", {}).get("message", "")
7072
if "Insufficient permissions for API method" in error_message:
71-
raise APIInsufficientPermissions(f"{error_message}{path_str}{headers_str}")
73+
log.error(f"{error_message}{path_str}{headers_str}")
74+
raise APIInsufficientPermissions()
7275
elif "Organization not allowed" in error_message:
73-
raise APIOrganizationNotAllowed(f"{error_message}{path_str}{headers_str}")
76+
log.error(f"{error_message}{path_str}{headers_str}")
77+
raise APIOrganizationNotAllowed()
7478
elif "Insufficient max quota" in error_message:
75-
raise APIInsufficientQuota(f"{error_message}{path_str}{headers_str}")
79+
log.error(f"{error_message}{path_str}{headers_str}")
80+
raise APIInsufficientQuota()
7681
else:
7782
raise APIAccessDenied(f"{error_message or 'Access denied'}{path_str}{headers_str}")
7883
except ValueError:
7984
raise APIAccessDenied(f"Access denied{path_str}{headers_str}")
8085
if response.status_code == 404:
81-
raise APIResourceNotFound(f"Path not found {path}{path_str}{headers_str}")
86+
log.error(f"Path not found {path}{path_str}{headers_str}")
87+
raise APIResourceNotFound()
8288
if response.status_code == 429:
8389
retry_after = response.headers.get("retry-after")
8490
if retry_after:
@@ -91,23 +97,28 @@ def format_headers(headers_dict):
9197
time_msg = f" Retry after: {retry_after}"
9298
else:
9399
time_msg = ""
94-
raise APIInsufficientQuota(f"Insufficient quota for API route.{time_msg}{path_str}{headers_str}")
100+
log.error(f"Insufficient quota for API route.{time_msg}{path_str}{headers_str}")
101+
raise APIInsufficientQuota()
95102
if response.status_code == 502:
96-
raise APIBadGateway(f"Upstream server error{path_str}{headers_str}")
103+
log.error(f"Upstream server error{path_str}{headers_str}")
104+
raise APIBadGateway()
97105
if response.status_code >= 400:
98-
raise APIFailure(
99-
f"Bad Request: HTTP original_status_code:{response.status_code}{path_str}{headers_str}",
100-
status_code=500,
106+
error = (
107+
f"Bad Request: HTTP original_status_code:{response.status_code}{path_str}{headers_str}"
101108
)
109+
log.error(error)
110+
raise APIFailure()
102111

103112
return response
104113

105114
except Timeout:
106115
request_duration = time.time() - start_time
107-
raise APITimeout(f"Request timed out after {request_duration:.2f} seconds")
116+
log.error(f"Request timed out after {request_duration:.2f} seconds")
117+
raise APITimeout()
108118
except ConnectionError as error:
109119
request_duration = time.time() - start_time
110-
raise APIConnectionError(f"Connection error after {request_duration:.2f} seconds: {error}")
120+
log.error(f"Connection error after {request_duration:.2f} seconds: {error}")
121+
raise APIConnectionError()
111122
except (
112123
APIAccessDenied,
113124
APIInsufficientQuota,
@@ -123,4 +134,5 @@ def format_headers(headers_dict):
123134
raise
124135
except Exception as error:
125136
# Only truly unexpected errors get wrapped in a generic APIFailure
126-
raise APIFailure(f"Unexpected error: {error}", status_code=500)
137+
log.error(f"Unexpected error: {error}")
138+
raise APIFailure()

socketdev/log.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import logging
2+
3+
log = logging.getLogger("socketdev")
4+
log.addHandler(logging.NullHandler())

socketdev/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.0.13"
1+
__version__ = "2.0.14"

0 commit comments

Comments
 (0)