Skip to content
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
5 changes: 4 additions & 1 deletion deepnote_toolkit/sql/sql_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@ def upload_sql_cache(dataframe, upload_url):
response = requests.put(upload_url, data=temp_file)
response.raise_for_status()
except Exception as exc:
detail = str(exc)
if isinstance(exc, requests.HTTPError) and exc.response is not None:
detail = f"{exc.response.status_code} {exc.response.text[:500]}"
logger.error(
"Failed to upload SQL cache: %s",
exc,
detail,
extra={"sql_caching_cause": "failed_to_upload_to_cache"},
)

Expand Down
35 changes: 35 additions & 0 deletions tests/unit/test_sql_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest.mock import patch

import pandas as pd
import requests
from parameterized import parameterized
from pyarrow import ArrowInvalid

Expand Down Expand Up @@ -394,3 +395,37 @@ def capture_file_state(f, **_kwargs):

self.assertEqual(pickle_pos, 0, "file should be at position 0")
self.assertEqual(pickle_size, 0, "file should be empty after truncate")

@patch("deepnote_toolkit.sql.sql_caching.logger")
@patch("deepnote_toolkit.sql.sql_caching.requests.put")
def test_http_error_logs_response_body(self, mock_put, mock_logger):
upload_url = "https://example.com/upload?signature=secret"
response = requests.Response()
response.status_code = 403
# raise_for_status() embeds response.url in str(exc), so the presigned
# URL only stays out of the log if we never format the exception itself
response.url = upload_url
response._content = (
b'<?xml version="1.0"?><Error>'
b"<Code>AccessDenied</Code>"
b"<Message>Request has expired</Message>"
b"</Error>"
)
mock_put.return_value = response
Comment thread
coderabbitai[bot] marked this conversation as resolved.

upload_sql_cache(pd.DataFrame({"a": [1]}), upload_url)

logged = mock_logger.error.call_args.args[1]
self.assertIn("403", logged)
self.assertIn("AccessDenied", logged)
self.assertNotIn(upload_url, logged)

@patch("deepnote_toolkit.sql.sql_caching.logger")
@patch("deepnote_toolkit.sql.sql_caching.requests.put")
def test_connection_error_logs_str_of_exception(self, mock_put, mock_logger):
mock_put.side_effect = requests.ConnectionError("connection timed out")

upload_sql_cache(pd.DataFrame({"a": [1]}), "https://example.com/upload")

logged = mock_logger.error.call_args.args[1]
self.assertIn("connection timed out", logged)
Loading