Skip to content

Commit

Permalink
Zapier - Add better error messaging for 401 responses (#6840)
Browse files Browse the repository at this point in the history
Description: When a 401 response is given back by Zapier, hint to the
end user why that may have occurred

- If an API Key was initialized with the wrapper, ask them to check
their API Key value
- if an access token was initialized with the wrapper, ask them to check
their access token or verify that it doesn't need to be refreshed.

Tag maintainer: @dev2049
  • Loading branch information
ralewis85 committed Jun 27, 2023
1 parent b24472e commit 74848aa
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
2 changes: 1 addition & 1 deletion langchain/utilities/searx_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def run(
str: The result of the query.
Raises:
ValueError: If an error occured with the query.
ValueError: If an error occurred with the query.
Example:
Expand Down
18 changes: 16 additions & 2 deletions langchain/utilities/zapier.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,22 @@ def list(self) -> List[Dict]:
https://nla.zapier.com/docs/using-the-api#ai-guessing)
"""
session = self._get_session()
response = session.get(self.zapier_nla_api_base + "exposed/")
response.raise_for_status()
try:
response = session.get(self.zapier_nla_api_base + "exposed/")
response.raise_for_status()
except requests.HTTPError as http_err:
if response.status_code == 401:
if self.zapier_nla_oauth_access_token:
raise requests.HTTPError(
f"An unauthorized response occurred. Check that your "
f"access token is correct and doesn't need to be "
f"refreshed. Err: {http_err}"
)
raise requests.HTTPError(
f"An unauthorized response occurred. Check that your api "
f"key is correct. Err: {http_err}"
)
raise http_err
return response.json()["results"]

def run(
Expand Down
66 changes: 66 additions & 0 deletions tests/unit_tests/tools/test_zapier.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Test building the Zapier tool, not running it."""
from unittest.mock import MagicMock, patch

import pytest
import requests

from langchain.tools.zapier.prompt import BASE_ZAPIER_TOOL_PROMPT
from langchain.tools.zapier.tool import ZapierNLARunAction
Expand Down Expand Up @@ -66,3 +69,66 @@ def test_wrapper_api_key_initialization() -> None:
def test_wrapper_access_token_initialization() -> None:
"""Test Wrapper initializes with an API Key."""
ZapierNLAWrapper(zapier_nla_oauth_access_token="test")


def test_list_raises_401_invalid_api_key() -> None:
"""Test that a valid error is raised when the API Key is invalid."""
mock_response = MagicMock()
mock_response.status_code = 401
mock_response.raise_for_status.side_effect = requests.HTTPError(
"401 Client Error: Unauthorized for url: https://nla.zapier.com/api/v1/exposed/"
)
mock_session = MagicMock()
mock_session.get.return_value = mock_response

with patch("requests.Session", return_value=mock_session):
wrapper = ZapierNLAWrapper(zapier_nla_api_key="test")

with pytest.raises(requests.HTTPError) as err:
wrapper.list()

assert str(err.value).startswith(
"An unauthorized response occurred. Check that your api key is correct. "
"Err:"
)


def test_list_raises_401_invalid_access_token() -> None:
"""Test that a valid error is raised when the API Key is invalid."""
mock_response = MagicMock()
mock_response.status_code = 401
mock_response.raise_for_status.side_effect = requests.HTTPError(
"401 Client Error: Unauthorized for url: https://nla.zapier.com/api/v1/exposed/"
)
mock_session = MagicMock()
mock_session.get.return_value = mock_response

with patch("requests.Session", return_value=mock_session):
wrapper = ZapierNLAWrapper(zapier_nla_oauth_access_token="test")

with pytest.raises(requests.HTTPError) as err:
wrapper.list()

assert str(err.value).startswith(
"An unauthorized response occurred. Check that your access token is "
"correct and doesn't need to be refreshed. Err:"
)


def test_list_raises_other_error() -> None:
"""Test that a valid error is raised when an unknown HTTP Error occurs."""
mock_response = MagicMock()
mock_response.status_code = 404
mock_response.raise_for_status.side_effect = requests.HTTPError(
"404 Client Error: Not found for url"
)
mock_session = MagicMock()
mock_session.get.return_value = mock_response

with patch("requests.Session", return_value=mock_session):
wrapper = ZapierNLAWrapper(zapier_nla_oauth_access_token="test")

with pytest.raises(requests.HTTPError) as err:
wrapper.list()

assert str(err.value) == "404 Client Error: Not found for url"

0 comments on commit 74848aa

Please sign in to comment.