exceptions: Add ResourceNotFoundError.

This commit is contained in:
PIG208
2021-07-04 14:52:23 +08:00
committed by Tim Abbott
parent a6e88a5a76
commit 6a04648fd7
5 changed files with 25 additions and 13 deletions

View File

@@ -386,3 +386,7 @@ class AccessDeniedError(JsonableError):
@staticmethod
def msg_format() -> str:
return _("Access denied")
class ResourceNotFoundError(JsonableError):
http_status_code = 404

View File

@@ -124,6 +124,7 @@ class TestIntegrationsDevPanel(ZulipTestCase):
target_url = "/devtools/integrations/somerandomnonexistantintegration/fixtures"
response = self.client_get(target_url)
expected_response = {
"code": "BAD_REQUEST",
"msg": '"somerandomnonexistantintegration" is not a valid webhook integration.',
"result": "error",
}
@@ -138,6 +139,7 @@ class TestIntegrationsDevPanel(ZulipTestCase):
target_url = "/devtools/integrations/airbrake/fixtures"
response = self.client_get(target_url)
expected_response = {
"code": "BAD_REQUEST",
"msg": 'The integration "airbrake" does not have fixtures.',
"result": "error",
}
@@ -319,6 +321,7 @@ class TestIntegrationsDevPanel(ZulipTestCase):
"/devtools/integrations/send_all_webhook_fixture_messages", data
)
expected_response = {
"code": "BAD_REQUEST",
"msg": 'The integration "appfollow" does not have fixtures.',
"result": "error",
}

View File

@@ -6,10 +6,10 @@ from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from django.test import Client
from zerver.lib.exceptions import JsonableError
from zerver.lib.exceptions import JsonableError, ResourceNotFoundError
from zerver.lib.integrations import WEBHOOK_INTEGRATIONS
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_error, json_success
from zerver.lib.response import json_success
from zerver.lib.validator import check_bool
from zerver.lib.webhooks.common import get_fixture_http_headers, standardize_headers
from zerver.models import UserProfile, get_realm
@@ -60,7 +60,7 @@ def send_webhook_fixture_message(
def get_fixtures(request: HttpResponse, integration_name: str = REQ()) -> HttpResponse:
valid_integration_name = get_valid_integration_name(integration_name)
if not valid_integration_name:
return json_error(f'"{integration_name}" is not a valid webhook integration.', status=404)
raise ResourceNotFoundError(f'"{integration_name}" is not a valid webhook integration.')
fixtures = {}
fixtures_dir = os.path.join(ZULIP_PATH, f"zerver/webhooks/{valid_integration_name}/fixtures")
@@ -68,7 +68,7 @@ def get_fixtures(request: HttpResponse, integration_name: str = REQ()) -> HttpRe
msg = ('The integration "{valid_integration_name}" does not have fixtures.').format(
valid_integration_name=valid_integration_name
)
return json_error(msg, status=404)
raise ResourceNotFoundError(msg)
for fixture in os.listdir(fixtures_dir):
fixture_path = os.path.join(fixtures_dir, fixture)
@@ -121,14 +121,14 @@ def send_all_webhook_fixture_messages(
) -> HttpResponse:
valid_integration_name = get_valid_integration_name(integration_name)
if not valid_integration_name:
return json_error(f'"{integration_name}" is not a valid webhook integration.', status=404)
raise ResourceNotFoundError(f'"{integration_name}" is not a valid webhook integration.')
fixtures_dir = os.path.join(ZULIP_PATH, f"zerver/webhooks/{valid_integration_name}/fixtures")
if not os.path.exists(fixtures_dir):
msg = ('The integration "{valid_integration_name}" does not have fixtures.').format(
valid_integration_name=valid_integration_name
)
return json_error(msg, status=404)
raise ResourceNotFoundError(msg)
responses = []
for fixture in os.listdir(fixtures_dir):

View File

@@ -7,10 +7,10 @@ from django.utils.translation import gettext as _
from zerver.lib.actions import recipient_for_user_profiles
from zerver.lib.addressee import get_user_profiles_by_ids
from zerver.lib.exceptions import JsonableError
from zerver.lib.exceptions import JsonableError, ResourceNotFoundError
from zerver.lib.message import normalize_body, truncate_topic
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_error, json_success
from zerver.lib.response import json_success
from zerver.lib.streams import access_stream_by_id
from zerver.lib.timestamp import timestamp_to_datetime
from zerver.lib.validator import (
@@ -128,7 +128,7 @@ def edit_draft(
try:
draft_object = Draft.objects.get(id=draft_id, user_profile=user_profile)
except Draft.DoesNotExist:
return json_error(_("Draft does not exist"), status=404)
raise ResourceNotFoundError(_("Draft does not exist"))
valid_draft_dict = further_validated_draft_dict(draft_dict, user_profile)
draft_object.content = valid_draft_dict["content"]
@@ -144,7 +144,7 @@ def delete_draft(request: HttpRequest, user_profile: UserProfile, draft_id: int)
try:
draft_object = Draft.objects.get(id=draft_id, user_profile=user_profile)
except Draft.DoesNotExist:
return json_error(_("Draft does not exist"), status=404)
raise ResourceNotFoundError(_("Draft does not exist"))
draft_object.delete()
return json_success()

View File

@@ -44,9 +44,14 @@ from zerver.lib.actions import (
internal_prep_private_message,
internal_prep_stream_message,
)
from zerver.lib.exceptions import ErrorCode, JsonableError, OrganizationOwnerRequired
from zerver.lib.exceptions import (
ErrorCode,
JsonableError,
OrganizationOwnerRequired,
ResourceNotFoundError,
)
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_error, json_success
from zerver.lib.response import json_success
from zerver.lib.retention import parse_message_retention_days
from zerver.lib.streams import (
StreamDict,
@@ -780,7 +785,7 @@ def json_stream_exists(
try:
(stream, sub) = access_stream_by_name(user_profile, stream_name)
except JsonableError as e:
return json_error(e.msg, status=404)
raise ResourceNotFoundError(e.msg)
# access_stream functions return a subscription if and only if we
# are already subscribed.