Use 'ParseError' instead of 'BadRequest' (#5625)

When we use `BadRequest` our responses doesn't contain error message, so
it's better to use exceptions from `rest_framework.exceptions`
main
Kirill Sizov 3 years ago committed by GitHub
parent d5f3f93e81
commit 7b86ed814e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,11 +7,10 @@ import os.path as osp
import functools import functools
import hashlib import hashlib
from django.core.exceptions import BadRequest
from django.utils.functional import SimpleLazyObject from django.utils.functional import SimpleLazyObject
from django.http import Http404, HttpResponseBadRequest, HttpResponseRedirect from django.http import Http404, HttpResponseBadRequest, HttpResponseRedirect
from rest_framework import views, serializers from rest_framework import views, serializers
from rest_framework.exceptions import ValidationError from rest_framework.exceptions import ValidationError, NotFound
from rest_framework.permissions import AllowAny from rest_framework.permissions import AllowAny
from rest_framework.decorators import api_view, permission_classes from rest_framework.decorators import api_view, permission_classes
from django.conf import settings from django.conf import settings
@ -70,10 +69,10 @@ def get_context(request):
org_header = request.headers.get('X-Organization') org_header = request.headers.get('X-Organization')
if org_id is not None and (org_slug is not None or org_header is not None): if org_id is not None and (org_slug is not None or org_header is not None):
raise BadRequest('You cannot specify "org_id" query parameter with ' raise ValidationError('You cannot specify "org_id" query parameter with '
'"org" query parameter or "X-Organization" HTTP header at the same time.') '"org" query parameter or "X-Organization" HTTP header at the same time.')
if org_slug is not None and org_header is not None and org_slug != org_header: if org_slug is not None and org_header is not None and org_slug != org_header:
raise BadRequest('You cannot specify "org" query parameter and ' raise ValidationError('You cannot specify "org" query parameter and '
'"X-Organization" HTTP header with different values.') '"X-Organization" HTTP header with different values.')
org_slug = org_slug if org_slug is not None else org_header org_slug = org_slug if org_slug is not None else org_header
@ -91,7 +90,7 @@ def get_context(request):
elif org_slug is not None: elif org_slug is not None:
org_filter = { 'organization': None } org_filter = { 'organization': None }
except Organization.DoesNotExist: except Organization.DoesNotExist:
raise BadRequest(f'{org_slug or org_id} organization does not exist.') raise NotFound(f'{org_slug or org_id} organization does not exist.')
if membership and not membership.is_active: if membership and not membership.is_active:
membership = None membership = None

@ -319,7 +319,7 @@ class TestPostWebhooks:
response = post_method("admin2", "webhooks", post_data, org_id=org_id) response = post_method("admin2", "webhooks", post_data, org_id=org_id)
assert response.status_code == HTTPStatus.BAD_REQUEST assert response.status_code == HTTPStatus.NOT_FOUND
def test_cannot_create_for_non_existent_project(self, projects): def test_cannot_create_for_non_existent_project(self, projects):
post_data = deepcopy(self.proj_webhook) post_data = deepcopy(self.proj_webhook)

Loading…
Cancel
Save