Skip to content

Commit 035b423

Browse files
Add email_verification_id field to BaseRequestException
Fixes #309 When authenticate_with_password() raises an AuthorizationException for an unverified email, the API response includes an email_verification_id field. This field is now properly extracted and accessible on the exception object, eliminating the need to manually parse the response JSON. Co-Authored-By: Deep Singhvi <[email protected]>
1 parent 15bf6e3 commit 035b423

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

tests/test_sync_http_client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,33 @@ def test_conflict_exception(self):
263263
assert str(ex) == "(message=No message, request_id=request-123)"
264264
assert ex.__class__ == ConflictException
265265

266+
def test_authorization_exception_includes_email_verification_id(self):
267+
request_id = "request-123"
268+
email_verification_id = "email_verification_01J6K4PMSWQXVFGF5ZQJXC6VC8"
269+
270+
self.http_client._client.request = MagicMock(
271+
return_value=httpx.Response(
272+
status_code=403,
273+
json={
274+
"message": "Please verify your email to authenticate via password.",
275+
"code": "email_verification_required",
276+
"email_verification_id": email_verification_id,
277+
},
278+
headers={"X-Request-ID": request_id},
279+
),
280+
)
281+
282+
try:
283+
self.http_client.request("bad_place")
284+
except AuthorizationException as ex:
285+
assert (
286+
ex.message == "Please verify your email to authenticate via password."
287+
)
288+
assert ex.code == "email_verification_required"
289+
assert ex.email_verification_id == email_verification_id
290+
assert ex.request_id == request_id
291+
assert ex.__class__ == AuthorizationException
292+
266293
def test_request_includes_base_headers(self, capture_and_mock_http_client_request):
267294
request_kwargs = capture_and_mock_http_client_request(self.http_client, {}, 200)
268295

workos/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def __init__(
2020
self.errors = self.extract_from_json("errors", None)
2121
self.code = self.extract_from_json("code", None)
2222
self.error_description = self.extract_from_json("error_description", "Unknown")
23+
self.email_verification_id = self.extract_from_json(
24+
"email_verification_id", None
25+
)
2326

2427
self.request_id = response.headers.get("X-Request-ID")
2528

0 commit comments

Comments
 (0)