Skip to content

Commit 78eb99e

Browse files
authored
Propagate ConflictExceptions properly (#389)
1 parent f72f68d commit 78eb99e

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

tests/test_async_http_client.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
from unittest.mock import AsyncMock
66

77
from tests.test_sync_http_client import STATUS_CODE_TO_EXCEPTION_MAPPING
8-
from workos.exceptions import BadRequestException, BaseRequestException, ServerException
8+
from workos.exceptions import (
9+
BadRequestException,
10+
BaseRequestException,
11+
ConflictException,
12+
ServerException,
13+
)
914
from workos.utils.http_client import AsyncHTTPClient
1015

1116

@@ -186,8 +191,6 @@ async def test_request_exceptions_include_expected_request_data(
186191
except expected_exception as ex: # type: ignore
187192
assert ex.message == response_message
188193
assert ex.request_id == request_id
189-
except Exception as ex:
190-
# This'll fail for sure here but... just using the nice error that'd come up
191194
assert ex.__class__ == expected_exception
192195

193196
async def test_bad_request_exceptions_include_expected_request_data(self):
@@ -210,7 +213,6 @@ async def test_bad_request_exceptions_include_expected_request_data(self):
210213
str(ex)
211214
== "(message=No message, request_id=request-123, error=example_error, error_description=Example error description)"
212215
)
213-
except Exception as ex:
214216
assert ex.__class__ == BadRequestException
215217

216218
async def test_bad_request_exceptions_exclude_expected_request_data(self):
@@ -228,7 +230,6 @@ async def test_bad_request_exceptions_exclude_expected_request_data(self):
228230
await self.http_client.request("bad_place")
229231
except BadRequestException as ex:
230232
assert str(ex) == "(message=No message, request_id=request-123, foo=bar)"
231-
except Exception as ex:
232233
assert ex.__class__ == BadRequestException
233234

234235
async def test_request_bad_body_raises_expected_exception_with_request_data(self):
@@ -247,10 +248,24 @@ async def test_request_bad_body_raises_expected_exception_with_request_data(self
247248
except ServerException as ex:
248249
assert ex.message == None
249250
assert ex.request_id == request_id
250-
except Exception as ex:
251-
# This'll fail for sure here but... just using the nice error that'd come up
252251
assert ex.__class__ == ServerException
253252

253+
async def test_conflict_exception(self):
254+
request_id = "request-123"
255+
256+
self.http_client._client.request = AsyncMock(
257+
return_value=httpx.Response(
258+
status_code=409,
259+
headers={"X-Request-ID": request_id},
260+
),
261+
)
262+
263+
try:
264+
await self.http_client.request("bad_place")
265+
except ConflictException as ex:
266+
assert str(ex) == "(message=No message, request_id=request-123)"
267+
assert ex.__class__ == ConflictException
268+
254269
async def test_request_includes_base_headers(
255270
self, capture_and_mock_http_client_request
256271
):

tests/test_sync_http_client.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
AuthorizationException,
1010
BadRequestException,
1111
BaseRequestException,
12+
ConflictException,
1213
ServerException,
1314
)
1415
from workos.utils.http_client import SyncHTTPClient
@@ -200,8 +201,6 @@ def test_request_exceptions_include_expected_request_data(
200201
except expected_exception as ex: # type: ignore
201202
assert ex.message == response_message
202203
assert ex.request_id == request_id
203-
except Exception as ex:
204-
# This'll fail for sure here but... just using the nice error that'd come up
205204
assert ex.__class__ == expected_exception
206205

207206
def test_bad_request_exceptions_include_request_data(self):
@@ -228,7 +227,6 @@ def test_bad_request_exceptions_include_request_data(self):
228227
str(ex)
229228
== "(message=No message, request_id=request-123, error=example_error, error_description=Example error description, foo=bar)"
230229
)
231-
except Exception as ex:
232230
assert ex.__class__ == BadRequestException
233231

234232
def test_request_bad_body_raises_expected_exception_with_request_data(self):
@@ -247,10 +245,24 @@ def test_request_bad_body_raises_expected_exception_with_request_data(self):
247245
except ServerException as ex:
248246
assert ex.message == None
249247
assert ex.request_id == request_id
250-
except Exception as ex:
251-
# This'll fail for sure here but... just using the nice error that'd come up
252248
assert ex.__class__ == ServerException
253249

250+
def test_conflict_exception(self):
251+
request_id = "request-123"
252+
253+
self.http_client._client.request = MagicMock(
254+
return_value=httpx.Response(
255+
status_code=409,
256+
headers={"X-Request-ID": request_id},
257+
),
258+
)
259+
260+
try:
261+
self.http_client.request("bad_place")
262+
except ConflictException as ex:
263+
assert str(ex) == "(message=No message, request_id=request-123)"
264+
assert ex.__class__ == ConflictException
265+
254266
def test_request_includes_base_headers(self, capture_and_mock_http_client_request):
255267
request_kwargs = capture_and_mock_http_client_request(self.http_client, {}, 200)
256268

workos/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class BadRequestException(BaseRequestException):
5353
pass
5454

5555

56+
class ConflictException(BaseRequestException):
57+
pass
58+
59+
5660
class NotFoundException(BaseRequestException):
5761
pass
5862

workos/utils/_base_http_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from httpx._types import QueryParamTypes
1717

1818
from workos.exceptions import (
19+
ConflictException,
1920
ServerException,
2021
AuthenticationException,
2122
AuthorizationException,
@@ -101,6 +102,8 @@ def _maybe_raise_error_by_status_code(
101102
raise AuthorizationException(response, response_json)
102103
elif status_code == 404:
103104
raise NotFoundException(response, response_json)
105+
elif status_code == 409:
106+
raise ConflictException(response, response_json)
104107

105108
raise BadRequestException(response, response_json)
106109
elif status_code >= 500 and status_code < 600:

0 commit comments

Comments
 (0)