Skip to content

Conversation

@jasonyuezhang
Copy link
Owner

If there is no org member associated with this user and organization, then we should exit gracefully.


Copied from getsentry#102113
Original PR: getsentry#102113

@propel-test-bot
Copy link

Gracefully handle missing OrganizationMember in metric alert notification target resolution

Adds a try/except OrganizationMember.DoesNotExist block around the lookup of the user-org relation when resolving a notification action target. If the user is no longer in the organization, the handler now returns None instead of raising, avoiding workflow crashes.

Key Changes

• Wrapped OrganizationMember.objects.get in a try/except OrganizationMember.DoesNotExist block
• Added inline comment explaining why the exception is ignored

Affected Areas

src/sentry/notifications/notification_action/group_type_notification_registry/handlers/metric_alert_registry_handler.py

This summary was automatically generated by @propel-code-bot

@propel-test-bot propel-test-bot bot changed the title chore(aci): handle OrganizationMember.DoesNotExist when getting target fix(aci): Gracefully handle missing OrganizationMember in MetricAlert target lookup Oct 26, 2025
Comment on lines +54 to +56
except OrganizationMember.DoesNotExist:
# user is no longer a member of the organization
pass

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[BestPractice]

The function now exits gracefully when OrganizationMember doesn't exist, but it doesn't return None explicitly. While Python functions return None by default, it's clearer to be explicit about the return value when handling the exception case.

Suggested change
except OrganizationMember.DoesNotExist:
# user is no longer a member of the organization
pass
except OrganizationMember.DoesNotExist:
# user is no longer a member of the organization
return None

Committable suggestion

Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Context for Agents
[**BestPractice**]

The function now exits gracefully when OrganizationMember doesn't exist, but it doesn't return None explicitly. While Python functions return None by default, it's clearer to be explicit about the return value when handling the exception case.

```suggestion
            except OrganizationMember.DoesNotExist:
                # user is no longer a member of the organization
                return None
```

⚡ **Committable suggestion**

Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

File: src/sentry/notifications/notification_action/group_type_notification_registry/handlers/metric_alert_registry_handler.py
Line: 56

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants