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

Parse raw_data on error only if the Content-Type is application/json. #91

Merged
merged 6 commits into from Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions opensearchpy/connection/base.py
Expand Up @@ -314,12 +314,13 @@ def log_request_fail(
if response is not None:
logger.debug("< %s", response)

def _raise_error(self, status_code, raw_data):
def _raise_error(self, status_code, raw_data, content_type=""):
"""Locate appropriate exception and raise it."""
error_message = raw_data
additional_info = None
try:
if raw_data:
content_type = content_type.split(";")[0].strip() or "application/json"
if raw_data and content_type == "application/json":
additional_info = json.loads(raw_data)
error_message = additional_info.get("error", error_message)
if isinstance(error_message, dict) and "type" in error_message:
Expand Down
4 changes: 3 additions & 1 deletion opensearchpy/connection/base.pyi
Expand Up @@ -112,6 +112,8 @@ class Connection(object):
response: Optional[str] = ...,
exception: Optional[Exception] = ...,
) -> None: ...
def _raise_error(self, status_code: int, raw_data: str) -> NoReturn: ...
def _raise_error(
self, status_code: int, raw_data: str, content_type: Optional[str]
axeoman marked this conversation as resolved.
Show resolved Hide resolved
) -> NoReturn: ...
def _get_default_user_agent(self) -> str: ...
def _get_api_key_header_val(self, api_key: Any) -> str: ...
6 changes: 5 additions & 1 deletion opensearchpy/connection/http_requests.py
Expand Up @@ -207,7 +207,11 @@ def perform_request(
response.status_code,
raw_data,
)
self._raise_error(response.status_code, raw_data)
self._raise_error(
response.status_code,
raw_data,
response.headers.get("Content-Type", ""),
)

self.log_request_success(
method,
Expand Down
9 changes: 8 additions & 1 deletion opensearchpy/connection/http_urllib3.py
Expand Up @@ -279,14 +279,21 @@ def perform_request(
self.log_request_fail(
method, full_url, url, orig_body, duration, response.status, raw_data
)
self._raise_error(response.status, raw_data)
self._raise_error(
response.status,
raw_data,
self.get_response_headers(response).get("content-type", ""),
)

self.log_request_success(
method, full_url, url, orig_body, response.status, raw_data, duration
)

return response.status, response.getheaders(), raw_data

def get_response_headers(self, response):
return {header.lower(): value for header, value in response.headers.items()}

def close(self):
"""
Explicitly closes connection
Expand Down
15 changes: 15 additions & 0 deletions test_opensearchpy/test_connection.py
Expand Up @@ -31,10 +31,12 @@
import os
import re
import ssl
import unittest
import warnings
from platform import python_version

import pytest
import six
import urllib3
from mock import Mock, patch
from requests.auth import AuthBase
Expand Down Expand Up @@ -163,6 +165,19 @@ def test_raises_warnings_when_folded(self):

self.assertEqual([str(w.message) for w in warn], ["warning", "folded"])

@unittest.skipIf(six.PY2, "not compatible with python2")
def test_raises_errors(self):
con = Connection()
with self.assertLogs("opensearch") as captured, self.assertRaises(
NotFoundError
):
con._raise_error(404, "Not found")
self.assertEqual(len(captured.output), 1)

# NB: this should assertNoLogs() but that method is not available until python3.10
with self.assertRaises(NotFoundError):
con._raise_error(404, "Not found", "text/plain; charset=UTF-8")

def test_ipv6_host_and_port(self):
for kwargs, expected_host in [
({"host": "::1"}, "http://[::1]:9200"),
Expand Down