Skip to content

Doug/fix api error #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions socketdev/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import logging

from socketdev.core.api import API
from socketdev.dependencies import Dependencies
from socketdev.export import Export
Expand All @@ -18,6 +16,7 @@
from socketdev.triage import Triage
from socketdev.utils import Utils, IntegrationType, INTEGRATION_TYPES
from socketdev.version import __version__
from socketdev.log import log


__author__ = "socket.dev"
Expand All @@ -30,8 +29,7 @@

api_url = "https://api.socket.dev/v0"
request_timeout = 1200
log = logging.getLogger("socketdev")
log.addHandler(logging.NullHandler())


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

Expand Down
36 changes: 24 additions & 12 deletions socketdev/core/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import base64
from socketdev.log import log

import requests
from socketdev.core.classes import Response
from socketdev.exceptions import (
Expand Down Expand Up @@ -68,17 +70,21 @@ def format_headers(headers_dict):
try:
error_message = response.json().get("error", {}).get("message", "")
if "Insufficient permissions for API method" in error_message:
raise APIInsufficientPermissions(f"{error_message}{path_str}{headers_str}")
log.error(f"{error_message}{path_str}{headers_str}")
raise APIInsufficientPermissions()
elif "Organization not allowed" in error_message:
raise APIOrganizationNotAllowed(f"{error_message}{path_str}{headers_str}")
log.error(f"{error_message}{path_str}{headers_str}")
raise APIOrganizationNotAllowed()
elif "Insufficient max quota" in error_message:
raise APIInsufficientQuota(f"{error_message}{path_str}{headers_str}")
log.error(f"{error_message}{path_str}{headers_str}")
raise APIInsufficientQuota()
else:
raise APIAccessDenied(f"{error_message or 'Access denied'}{path_str}{headers_str}")
except ValueError:
raise APIAccessDenied(f"Access denied{path_str}{headers_str}")
if response.status_code == 404:
raise APIResourceNotFound(f"Path not found {path}{path_str}{headers_str}")
log.error(f"Path not found {path}{path_str}{headers_str}")
raise APIResourceNotFound()
if response.status_code == 429:
retry_after = response.headers.get("retry-after")
if retry_after:
Expand All @@ -91,23 +97,28 @@ def format_headers(headers_dict):
time_msg = f" Retry after: {retry_after}"
else:
time_msg = ""
raise APIInsufficientQuota(f"Insufficient quota for API route.{time_msg}{path_str}{headers_str}")
log.error(f"Insufficient quota for API route.{time_msg}{path_str}{headers_str}")
raise APIInsufficientQuota()
if response.status_code == 502:
raise APIBadGateway(f"Upstream server error{path_str}{headers_str}")
log.error(f"Upstream server error{path_str}{headers_str}")
raise APIBadGateway()
if response.status_code >= 400:
raise APIFailure(
f"Bad Request: HTTP original_status_code:{response.status_code}{path_str}{headers_str}",
status_code=500,
error = (
f"Bad Request: HTTP original_status_code:{response.status_code}{path_str}{headers_str}"
)
log.error(error)
raise APIFailure()

return response

except Timeout:
request_duration = time.time() - start_time
raise APITimeout(f"Request timed out after {request_duration:.2f} seconds")
log.error(f"Request timed out after {request_duration:.2f} seconds")
raise APITimeout()
except ConnectionError as error:
request_duration = time.time() - start_time
raise APIConnectionError(f"Connection error after {request_duration:.2f} seconds: {error}")
log.error(f"Connection error after {request_duration:.2f} seconds: {error}")
raise APIConnectionError()
except (
APIAccessDenied,
APIInsufficientQuota,
Expand All @@ -123,4 +134,5 @@ def format_headers(headers_dict):
raise
except Exception as error:
# Only truly unexpected errors get wrapped in a generic APIFailure
raise APIFailure(f"Unexpected error: {error}", status_code=500)
log.error(f"Unexpected error: {error}")
raise APIFailure()
4 changes: 4 additions & 0 deletions socketdev/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import logging

log = logging.getLogger("socketdev")
log.addHandler(logging.NullHandler())
2 changes: 1 addition & 1 deletion socketdev/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.0.13"
__version__ = "2.0.14"
Loading