Skip to content
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
22 changes: 14 additions & 8 deletions packages/apps/src/microsoft_teams/apps/app_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,25 @@ async def sign_in_token_exchange(
self.event_emitter.emit("sign_in", SignInEvent(activity_ctx=ctx, token_response=token))
return None
except Exception as e:
ctx.logger.error(
f"Error exchanging token for user {activity.from_.id} in "
f"conversation {activity.conversation.id}: {e}"
)
if isinstance(e, HTTPStatusError):
status = e.response.status_code
if status not in (404, 400, 412):
log.error(
f"Error exchanging token for user {activity.from_.id} in "
f"conversation {activity.conversation.id}: {e}"
)
self.event_emitter.emit("error", ErrorEvent(error=e, context={"activity": activity}))
return InvokeResponse(status=status or 500)
ctx.logger.warning(
f"Unable to exchange token for user {activity.from_.id} in "
f"conversation {activity.conversation.id}: {e}"
)
log.info(
f"Unable to exchange token for user {activity.from_.id} in "
f"conversation {activity.conversation.id}: {e}"
)
else:
log.error(
f"Unable to exchange token for user {activity.from_.id} in "
f"conversation {activity.conversation.id}: {e}"
)
self.event_emitter.emit("error", ErrorEvent(error=e, context={"activity": activity}))
return InvokeResponse(
status=412,
body=TokenExchangeInvokeResponse(
Expand Down
9 changes: 4 additions & 5 deletions packages/apps/tests/test_app_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,6 @@ async def test_sign_in_token_exchange_http_error_404(self, oauth_handlers, mock_
# Verify no error event emitted for 404
oauth_handlers.event_emitter.emit.assert_not_called()

# Verify warning logged
mock_context.logger.warning.assert_called_once()

# Verify failure response
assert isinstance(result, InvokeResponse) and isinstance(result.body, TokenExchangeInvokeResponse)
assert result.status == 412
Expand Down Expand Up @@ -215,8 +212,10 @@ async def test_sign_in_token_exchange_generic_exception(

result = await oauth_handlers.sign_in_token_exchange(mock_context)

# Verify warning logged
mock_context.logger.warning.assert_called_once()
# Verify error event emitted for non-HTTP exceptions
oauth_handlers.event_emitter.emit.assert_called_once_with(
"error", ErrorEvent(error=generic_error, context={"activity": token_exchange_activity})
)

# Verify failure response
assert isinstance(result, InvokeResponse) and isinstance(result.body, TokenExchangeInvokeResponse)
Expand Down