Skip to content

Commit 61f406c

Browse files
JoshFergepriscilawebdev
authored andcommitted
ref: remove coreapi, use ParseError in sentry_app logic (#97913)
removes the coreapi module. The only export that was used was `APIError`, and we can consolidate this onto `rest_framework.exceptions.ParseError`.
1 parent ed6220b commit 61f406c

File tree

9 files changed

+15
-78
lines changed

9 files changed

+15
-78
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,6 @@ module = [
668668
"tests.sentry.auth.services.*",
669669
"tests.sentry.celery.*",
670670
"tests.sentry.charts.*",
671-
"tests.sentry.coreapi.*",
672671
"tests.sentry.data_export.processors.*",
673672
"tests.sentry.data_secrecy.*",
674673
"tests.sentry.debug.*",

src/sentry/coreapi.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/sentry/sentry_apps/api/bases/sentryapps.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import logging
44
from collections.abc import Sequence
5-
from functools import wraps
65
from typing import Any
76

87
import sentry_sdk
@@ -15,7 +14,6 @@
1514
from sentry.api.permissions import SentryPermission, StaffPermissionMixin
1615
from sentry.auth.staff import is_active_staff
1716
from sentry.auth.superuser import is_active_superuser, superuser_has_permission
18-
from sentry.coreapi import APIError
1917
from sentry.integrations.api.bases.integration import PARANOID_GET
2018
from sentry.models.organization import OrganizationStatus
2119
from sentry.organizations.services.organization import (
@@ -39,17 +37,6 @@
3937
logger = logging.getLogger(__name__)
4038

4139

42-
def catch_raised_errors(func):
43-
@wraps(func)
44-
def wrapped(self, *args, **kwargs):
45-
try:
46-
return func(self, *args, **kwargs)
47-
except APIError as e:
48-
return Response({"detail": e.msg}, status=400)
49-
50-
return wrapped
51-
52-
5340
def ensure_scoped_permission(request: Request, allowed_scopes: Sequence[str] | None) -> bool:
5441
"""
5542
Verifies the User making the request has at least one required scope for

src/sentry/sentry_apps/api/endpoints/sentry_app_details.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from sentry.sentry_apps.api.bases.sentryapps import (
2424
SentryAppAndStaffPermission,
2525
SentryAppBaseEndpoint,
26-
catch_raised_errors,
2726
)
2827
from sentry.sentry_apps.api.parsers.sentry_app import SentryAppParser
2928
from sentry.sentry_apps.api.serializers.sentry_app import (
@@ -99,7 +98,6 @@ def get(self, request: Request, sentry_app) -> Response:
9998
},
10099
examples=SentryAppExamples.UPDATE_SENTRY_APP,
101100
)
102-
@catch_raised_errors
103101
def put(self, request: Request, sentry_app) -> Response:
104102
"""
105103
Update an existing custom integration.

src/sentry/sentry_apps/logic.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010
from django.db.models import Q
1111
from django.http.request import HttpRequest
1212
from django.utils import timezone
13-
from rest_framework.exceptions import ValidationError
13+
from rest_framework.exceptions import ParseError, ValidationError
1414
from sentry_sdk.api import isolation_scope
1515

1616
from sentry import analytics, audit_log
1717
from sentry.api.helpers.slugs import sentry_slugify
1818
from sentry.auth.staff import has_staff_option
1919
from sentry.constants import SentryAppStatus
20-
from sentry.coreapi import APIError
2120
from sentry.db.postgres.transactions import in_test_hide_transaction_boundary
2221
from sentry.integrations.models.integration_feature import IntegrationFeature, IntegrationTypes
2322
from sentry.models.apiapplication import ApiApplication
@@ -141,7 +140,7 @@ def run(self, user: User | RpcUser) -> SentryApp:
141140
def _update_features(self, user: User | RpcUser) -> None:
142141
if self.features is not None:
143142
if not _is_elevated_user(user) and self.sentry_app.status == SentryAppStatus.PUBLISHED:
144-
raise APIError("Cannot update features on a published integration.")
143+
raise ParseError(detail="Cannot update features on a published integration.")
145144

146145
IntegrationFeature.objects.clean_update(
147146
incoming_features=self.features,
@@ -173,7 +172,7 @@ def _update_scopes(self) -> None:
173172
if self.sentry_app.status == SentryAppStatus.PUBLISHED and set(
174173
self.sentry_app.scope_list
175174
) != set(self.scopes):
176-
raise APIError("Cannot update permissions on a published integration.")
175+
raise ParseError(detail="Cannot update permissions on a published integration.")
177176

178177
# We are using a pre_save signal to enforce scope hierarchy on the ApiToken model.
179178
# Because we're using bulk_update here to update all the tokens for the SentryApp,
@@ -198,7 +197,9 @@ def _update_events(self) -> None:
198197
for event in self.events:
199198
needed_scope = REQUIRED_EVENT_PERMISSIONS[event]
200199
if needed_scope not in self.sentry_app.scope_list:
201-
raise APIError(f"{event} webhooks require the {needed_scope} permission.")
200+
raise ParseError(
201+
detail=f"{event} webhooks require the {needed_scope} permission."
202+
)
202203

203204
self.sentry_app.events = expand_events(self.events)
204205

@@ -243,7 +244,7 @@ def _update_is_alertable(self) -> None:
243244
def _update_verify_install(self) -> None:
244245
if self.verify_install is not None:
245246
if self.sentry_app.is_internal and self.verify_install:
246-
raise APIError("Internal integrations cannot have verify_install=True.")
247+
raise ParseError(detail="Internal integrations cannot have verify_install=True.")
247248
self.sentry_app.verify_install = self.verify_install
248249

249250
def _update_overview(self) -> None:

src/sentry/sentry_apps/models/sentry_app_installation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from django.utils import timezone
99
from jsonschema import ValidationError
1010

11+
from sentry.api.exceptions import BadRequest
1112
from sentry.auth.services.auth import AuthenticatedToken
1213
from sentry.backup.scopes import RelocationScope
1314
from sentry.constants import SentryAppInstallationStatus
@@ -235,7 +236,6 @@ def prepare_ui_component(
235236
project_slug: str | None = None,
236237
values: list[Mapping[str, Any]] | None = None,
237238
) -> SentryAppComponent | RpcSentryAppComponent | None:
238-
from sentry.coreapi import APIError
239239
from sentry.sentry_apps.components import SentryAppComponentPreparer
240240

241241
if values is None:
@@ -246,7 +246,7 @@ def prepare_ui_component(
246246
).run()
247247
return component
248248
except (
249-
APIError,
249+
BadRequest,
250250
ValidationError,
251251
SentryAppIntegratorError,
252252
SentryAppError,

tests/sentry/coreapi/test_coreapi.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/sentry/sentry_apps/api/endpoints/test_sentry_app_components.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
from unittest.mock import MagicMock, patch
33

44
import responses
5+
from rest_framework.exceptions import ParseError
56

67
from sentry.api.serializers.base import serialize
78
from sentry.constants import SentryAppInstallationStatus
8-
from sentry.coreapi import APIError
99
from sentry.sentry_apps.models.sentry_app import SentryApp
1010
from sentry.sentry_apps.models.sentry_app_component import SentryAppComponent
1111
from sentry.sentry_apps.utils.errors import SentryAppIntegratorError, SentryAppSentryError
@@ -370,7 +370,7 @@ def test_component_prep_general_error(
370370
def test_component_prep_errors_dont_bring_down_everything(
371371
self, run: MagicMock, capture_exception: MagicMock
372372
) -> None:
373-
run.side_effect = [APIError(), SentryAppSentryError(message="kewl")]
373+
run.side_effect = [ParseError(), SentryAppSentryError(message="kewl")]
374374
capture_exception.return_value = 1
375375

376376
response = self.get_success_response(

tests/sentry/sentry_apps/test_sentry_app_updater.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
import pytest
55
from django.utils import timezone
6+
from rest_framework.exceptions import ParseError
67

78
from sentry.constants import SentryAppStatus
8-
from sentry.coreapi import APIError
99
from sentry.integrations.types import EventLifecycleOutcome
1010
from sentry.models.apitoken import ApiToken
1111
from sentry.sentry_apps.logic import SentryAppUpdater, expand_events
@@ -100,7 +100,7 @@ def test_doesnt_update_published_app_scopes(self) -> None:
100100
updater = SentryAppUpdater(sentry_app=sentry_app)
101101
updater.scopes = ["project:read", "project:write"]
102102

103-
with pytest.raises(APIError):
103+
with pytest.raises(ParseError):
104104
updater.run(self.user)
105105

106106
def test_update_webhook_published_app(self) -> None:
@@ -120,15 +120,15 @@ def test_doesnt_update_app_with_invalid_event_permissions(self) -> None:
120120
)
121121
updater = SentryAppUpdater(sentry_app=sentry_app)
122122
updater.events = ["issue"]
123-
with pytest.raises(APIError):
123+
with pytest.raises(ParseError):
124124
updater.run(self.user)
125125

126126
def test_doesnt_update_verify_install_if_internal(self) -> None:
127127
self.create_project(organization=self.org)
128128
sentry_app = self.create_internal_integration(name="Internal", organization=self.org)
129129
updater = SentryAppUpdater(sentry_app=sentry_app)
130130
updater.verify_install = True
131-
with pytest.raises(APIError):
131+
with pytest.raises(ParseError):
132132
updater.run(self.user)
133133

134134
def test_updates_service_hook_events(self) -> None:

0 commit comments

Comments
 (0)