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

chore(aci milestone 3): handle exceptions in migration helpers #85079

Merged
merged 4 commits into from
Feb 18, 2025

Conversation

mifu67
Copy link
Contributor

@mifu67 mifu67 commented Feb 12, 2025

Changes to exception handling for migration helpers.

  • If action type is invalid, raise a ValidationError instead of a custom exception, because this is user error
  • All other exceptions remain uncaught
    Wrote some TODOs to wrap the dual write/update/delete calls in transactions, but this is outside the scope of this PR.

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Feb 12, 2025
Copy link

codecov bot commented Feb 12, 2025

Codecov Report

Attention: Patch coverage is 68.42105% with 6 lines in your changes missing coverage. Please review.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...dents/endpoints/organization_alert_rule_details.py 57.14% 3 Missing ⚠️
...incidents/serializers/alert_rule_trigger_action.py 60.00% 2 Missing ⚠️
...ry/workflow_engine/migration_helpers/alert_rule.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master   #85079   +/-   ##
=======================================
  Coverage   87.94%   87.94%           
=======================================
  Files        9634     9634           
  Lines      544128   544138   +10     
  Branches    21124    21124           
=======================================
+ Hits       478528   478537    +9     
- Misses      65295    65296    +1     
  Partials      305      305           

Copy link
Contributor

@saponifi3d saponifi3d left a comment

Choose a reason for hiding this comment

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

I think the response / bad request blocks are probably okay, but i think we can remove the ones that just log and re-raise.

please let me know if you have any questions!

extra={"details": str(e)},
)
return Response(
"Error when dual deleting alert rule", status=status.HTTP_400_BAD_REQUEST
Copy link
Contributor

Choose a reason for hiding this comment

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

We may want to look at the kind of exception to decide the status code here -- 400 errors are mean that they're caused by bad user input.

Since there are a lot of possible errors that can arise when we are deleting these rules, we should check the exception to see if it was caused by user input (4xx errors) or something on our end (5xx errors).

side note for the best of the http status codes, 418

src/sentry/incidents/logic.py Outdated Show resolved Hide resolved
logger.exception(
"Error when dual writing alert rule", extra={"details": str(e)}
)
raise BadRequest
Copy link
Contributor

Choose a reason for hiding this comment

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

it might be nice if the exception included why it failed, then proxy that through the BadRequest so the API can have more specific errors.

@mifu67 mifu67 requested a review from saponifi3d February 13, 2025 19:46
migrate_metric_action(action)
except ValidationError as e:
# invalid action type
raise serializers.ValidationError(str(e))

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.

Copilot Autofix AI 5 days ago

To fix the problem, we need to ensure that the detailed error message is logged on the server, and a generic error message is returned to the user. This can be achieved by logging the exception details and raising a generic serializers.ValidationError with a non-specific message.

  • Modify the create method to log the detailed exception message and raise a generic error message.
  • Modify the update method similarly to ensure consistency.
Suggested changeset 1
src/sentry/incidents/serializers/alert_rule_trigger_action.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/sentry/incidents/serializers/alert_rule_trigger_action.py b/src/sentry/incidents/serializers/alert_rule_trigger_action.py
--- a/src/sentry/incidents/serializers/alert_rule_trigger_action.py
+++ b/src/sentry/incidents/serializers/alert_rule_trigger_action.py
@@ -1,2 +1,5 @@
 from django.forms import ValidationError
+import logging
+
+logger = logging.getLogger(__name__)
 from django.utils.encoding import force_str
@@ -220,3 +223,4 @@
                 # invalid action type
-                raise serializers.ValidationError(str(e))
+                logger.error("Validation error during metric action migration: %s", str(e))
+                raise serializers.ValidationError("An internal error has occurred.")
 
@@ -231,3 +235,4 @@
         except (ApiRateLimitedError, InvalidTriggerActionError) as e:
-            raise serializers.ValidationError(force_str(e))
+            logger.error("Error updating alert rule trigger action: %s", force_str(e))
+            raise serializers.ValidationError("An internal error has occurred.")
 
EOF
@@ -1,2 +1,5 @@
from django.forms import ValidationError
import logging

logger = logging.getLogger(__name__)
from django.utils.encoding import force_str
@@ -220,3 +223,4 @@
# invalid action type
raise serializers.ValidationError(str(e))
logger.error("Validation error during metric action migration: %s", str(e))
raise serializers.ValidationError("An internal error has occurred.")

@@ -231,3 +235,4 @@
except (ApiRateLimitedError, InvalidTriggerActionError) as e:
raise serializers.ValidationError(force_str(e))
logger.error("Error updating alert rule trigger action: %s", force_str(e))
raise serializers.ValidationError("An internal error has occurred.")

Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
@mifu67 mifu67 marked this pull request as ready for review February 18, 2025 19:53
@mifu67 mifu67 requested review from a team as code owners February 18, 2025 19:53
@mifu67 mifu67 merged commit 1f2291c into master Feb 18, 2025
50 checks passed
@mifu67 mifu67 deleted the mifu67/aci/handle-helper-exceptions branch February 18, 2025 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants