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

feat: remove PassageError error message prefixes #136

Merged
merged 1 commit into from
Jan 9, 2025
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
3 changes: 1 addition & 2 deletions passageidentity/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,4 @@ def create_magic_link(self, args: MagicLinkArgs, options: MagicLinkOptions | Non
_headers=self.request_headers,
).magic_link
except ApiException as e:
msg = "Could not create a magic link for this app"
raise PassageError.from_response_error(e, msg) from e
raise PassageError.from_response_error(e) from e
7 changes: 3 additions & 4 deletions passageidentity/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@ def __str__(self) -> str:
return self.message

@classmethod
def from_response_error(cls, response_error: ApiException, message: str | None = None) -> PassageError:
def from_response_error(cls, response_error: ApiException) -> PassageError:
"""Initialize the error with a response body and optional message."""
if response_error.data is not None:
data_dict = response_error.data.to_dict()
error_code = data_dict.get("code")
error_msg = data_dict.get("error")
msg = ": ".join(filter(None, [message, error_msg]))
else:
error_code = None
msg = str(response_error.body)
error_msg = str(response_error.body)

psg_error = cls()
psg_error.message = msg
psg_error.message = error_msg
psg_error.status_code = response_error.status
psg_error.error_code = error_code

Expand Down
30 changes: 10 additions & 20 deletions passageidentity/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ def get(self, user_id: str) -> PassageUser:
try:
return self.users_api.get_user(self.app_id, user_id, _headers=self.request_headers).user
except ApiException as e:
msg = "Could not fetch user"
raise PassageError.from_response_error(e, msg) from e
raise PassageError.from_response_error(e) from e

def get_by_identifier(self, identifier: str) -> PassageUser:
"""Get a user's object using their user identifier."""
Expand All @@ -60,8 +59,7 @@ def get_by_identifier(self, identifier: str) -> PassageUser:
_headers=self.request_headers,
).users
except ApiException as e:
msg = "Could not fetch user by identifier"
raise PassageError.from_response_error(e, msg) from e
raise PassageError.from_response_error(e) from e

if len(users) == 0:
raise PassageError.from_response_error(
Expand All @@ -79,8 +77,7 @@ def activate(self, user_id: str) -> PassageUser:
try:
return self.users_api.activate_user(self.app_id, user_id, _headers=self.request_headers).user
except ApiException as e:
msg = "Could not activate user"
raise PassageError.from_response_error(e, msg) from e
raise PassageError.from_response_error(e) from e

def deactivate(self, user_id: str) -> PassageUser:
"""Deactivate a user using their user ID."""
Expand All @@ -91,8 +88,7 @@ def deactivate(self, user_id: str) -> PassageUser:
try:
return self.users_api.deactivate_user(self.app_id, user_id, _headers=self.request_headers).user
except ApiException as e:
msg = "Could not deactivate user"
raise PassageError.from_response_error(e, msg) from e
raise PassageError.from_response_error(e) from e

def update(self, user_id: str, options: UpdateUserArgs) -> PassageUser:
"""Update a user."""
Expand All @@ -103,8 +99,7 @@ def update(self, user_id: str, options: UpdateUserArgs) -> PassageUser:
try:
return self.users_api.update_user(self.app_id, user_id, options, _headers=self.request_headers).user
except ApiException as e:
msg = "Could not update user"
raise PassageError.from_response_error(e, msg) from e
raise PassageError.from_response_error(e) from e

def create(self, args: CreateUserArgs) -> PassageUser:
"""Create a user."""
Expand All @@ -115,8 +110,7 @@ def create(self, args: CreateUserArgs) -> PassageUser:
try:
return self.users_api.create_user(self.app_id, args, _headers=self.request_headers).user
except ApiException as e:
msg = "Could not create user"
raise PassageError.from_response_error(e, msg) from e
raise PassageError.from_response_error(e) from e

def delete(self, user_id: str) -> None:
"""Delete a user using their user ID."""
Expand All @@ -127,8 +121,7 @@ def delete(self, user_id: str) -> None:
try:
self.users_api.delete_user(self.app_id, user_id, _headers=self.request_headers)
except ApiException as e:
msg = "Could not delete user"
raise PassageError.from_response_error(e, msg) from e
raise PassageError.from_response_error(e) from e

def list_devices(self, user_id: str) -> list[WebAuthnDevices]:
"""Get a user's devices using their user ID."""
Expand All @@ -139,8 +132,7 @@ def list_devices(self, user_id: str) -> list[WebAuthnDevices]:
try:
return self.user_devices_api.list_user_devices(self.app_id, user_id, _headers=self.request_headers).devices
except ApiException as e:
msg = "Could not fetch user's devices"
raise PassageError.from_response_error(e, msg) from e
raise PassageError.from_response_error(e) from e

def revoke_device(self, user_id: str, device_id: str) -> None:
"""Revoke a user's device using their user ID and the device ID."""
Expand All @@ -155,8 +147,7 @@ def revoke_device(self, user_id: str, device_id: str) -> None:
try:
self.user_devices_api.delete_user_devices(self.app_id, user_id, device_id, _headers=self.request_headers)
except ApiException as e:
msg = "Could not revoke user's device"
raise PassageError.from_response_error(e, msg) from e
raise PassageError.from_response_error(e) from e

def revoke_refresh_tokens(self, user_id: str) -> None:
"""Revokes all of a user's Refresh Tokens using their User ID."""
Expand All @@ -167,5 +158,4 @@ def revoke_refresh_tokens(self, user_id: str) -> None:
try:
self.tokens_api.revoke_user_refresh_tokens(self.app_id, user_id, _headers=self.request_headers)
except ApiException as e:
msg = "Could not revoke user's refresh tokens"
raise PassageError.from_response_error(e, msg) from e
raise PassageError.from_response_error(e) from e
12 changes: 0 additions & 12 deletions tests/errors_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@ def test_from_response_error() -> None:
data=Model400Error(code="invalid_request", error="some error"),
)

error = PassageError.from_response_error(response_error, "some message")
assert error.message == "some message: some error"
assert error.status_code == 400
assert error.error_code == "invalid_request"


def test_from_response_error_without_message() -> None:
response_error = MockApiException(
status=400,
data=Model400Error(code="invalid_request", error="some error"),
)

error = PassageError.from_response_error(response_error)
assert error.message == "some error"
assert error.status_code == 400
Expand Down
Loading