Skip to content
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

improve http error handling #799

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion bot/kodiak/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import sentry_sdk
import structlog
from requests import Response
from httpx import Response
from sentry_sdk import capture_event
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.utils import event_from_exception
Expand Down
11 changes: 8 additions & 3 deletions bot/kodiak/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,15 @@ async def evaluate_pr(
),
timeout=60,
)
if pr is None:
log.info("failed to get_pr")
return
try:
if pr is None:
if merging:
raise ApiCallException(
method="kodiak/get_pull_request",
http_status_code=0,
response=b"",
)
return None
await asyncio.wait_for(
mergeable(
api=pr,
Expand Down
15 changes: 10 additions & 5 deletions bot/kodiak/queries/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ async def send_query(
query: str,
variables: Mapping[str, Union[str, int, None]],
installation_id: str,
) -> Optional[GraphQLResponse]:
) -> GraphQLResponse | http.HTTPError:
log = self.log

token = await get_token_for_install(
Expand All @@ -857,9 +857,9 @@ async def send_query(
log = log.bind(rate_limit=rate_limit)
try:
res.raise_for_status()
except http.HTTPError:
except http.HTTPError as e:
log.warning("github api request error", res=res, exc_info=True)
return None
return e
return cast(GraphQLResponse, res.json())

async def get_api_features(self) -> ApiFeatures | None:
Expand Down Expand Up @@ -935,6 +935,7 @@ def get_bot_reviews(self, *, reviews: List[PRReviewSchema]) -> List[PRReview]:
async def get_config_for_ref(
self, *, ref: str, org_repo_default_branch: str | None
) -> CfgInfo | None:
log = self.log.bind(ref=ref, org_repo_default_branch=org_repo_default_branch)
repo_root_config_expression = create_root_config_file_expression(branch=ref)
repo_github_config_expression = create_github_config_file_expression(branch=ref)
org_root_config_expression: str | None = None
Expand All @@ -959,15 +960,18 @@ async def get_config_for_ref(
),
installation_id=self.installation_id,
)
log = log.bind(res=res)
if res is None:
log.info("get_config api error")
return None
data = res.get("data")
if data is None:
self.log.error("could not fetch default branch name", res=res)
log.error("could not fetch default branch name")
return None

parsed_config = parse_config(data)
if parsed_config is None:
log.info("no config in response")
return None

def get_file_expression() -> str:
Expand All @@ -990,7 +994,7 @@ def get_file_expression() -> str:
file_expression=get_file_expression(),
)

async def get_event_info(self, pr_number: int) -> Optional[EventInfoResponse]:
async def get_event_info(self, pr_number: int) -> EventInfoResponse:
"""
Retrieve all the information we need to evaluate a pull request

Expand All @@ -1011,6 +1015,7 @@ async def get_event_info(self, pr_number: int) -> Optional[EventInfoResponse]:
installation_id=self.installation_id,
)
if res is None:
log.warning("empty API response")
return None

data = res.get("data")
Expand Down
16 changes: 7 additions & 9 deletions bot/kodiak/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, cast

import pytest
from requests import PreparedRequest, Request, Response
from httpx import Request, Response

from kodiak.logging import (
SentryLevel,
Expand Down Expand Up @@ -160,14 +160,12 @@ def test_add_request_info_processor() -> None:
url = "https://api.example.com/v1/me"
payload = dict(user_id=54321)
req = Request("POST", url, json=payload)
res = Response()
res.status_code = 500
res.url = url
res.reason = "Internal Server Error"
cast(
Any, res
)._content = b"Your request could not be completed due to an internal error."
res.request = cast(PreparedRequest, req.prepare()) # type: ignore
res = Response(
status_code=500,
content=b"Your request could not be completed due to an internal error.",
request=req,
)

event_dict = add_request_info_processor(
None, None, dict(event="request failed", res=res)
)
Expand Down
18 changes: 17 additions & 1 deletion bot/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bot/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ flake8-pie = "0.7.1"
isort = "^4.3"
pytest-cov = "^2.10"
flake8-pyi = "^20.10"
flake8-tidy-imports = "^4.6.0"

[tool.poetry.plugins."pytest11"]
"pytest_plugin" = "pytest_plugin.plugin"
Expand Down
3 changes: 3 additions & 0 deletions bot/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ filterwarnings =
ignore::pytest.PytestDeprecationWarning
ignore::DeprecationWarning:asyncio_redis
[flake8]
banned-modules =
requests = Use httpx instead.,
ban-relative-imports = true
ignore =
; formatting handled by black
; https://pycodestyle.readthedocs.io/en/latest/intro.html#error-codes
Expand Down
3 changes: 3 additions & 0 deletions kodiak.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
},
{
"path": "infrastructure"
},
{
"path": "."
}
],
"settings": {}
Expand Down