Skip to content

Catch ValueErrors when selecting by "id" #55

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

Open
ddobson opened this issue Apr 23, 2019 · 0 comments
Open

Catch ValueErrors when selecting by "id" #55

ddobson opened this issue Apr 23, 2019 · 0 comments

Comments

@ddobson
Copy link
Contributor

ddobson commented Apr 23, 2019

Describe the bug

When passing IDs in the relationships object of a POST/PATCH request, passing an invalid data type will throw a ValueError and result in a 500.

Add ValueError to the except block of this function: https://github.com/Vacasa/drf-jsonapi/blob/master/drf_jsonapi/serializers/resources.py#L464

To Reproduce
Steps to reproduce the behavior:

  1. Make a POST/PATCH request to a resources with related objects. In the data.relationships.resource.data.id key, pass an invalid type for an ID. For instance, a string such as "oops" for an ID represented by an integer.
  2. Observe a 500 response

Expected behavior

Catch the error and return a 400 with an error message indicating the

Screenshots

ValueError at /unit-features
invalid literal for int() with base 10: 'asklfj'

Request Method: POST

Traceback:

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python3.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
  54.         return view_func(*args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/rest_framework/viewsets.py" in view
  103.             return self.dispatch(request, *args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
  483.             response = self.handle_exception(exc)

File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py" in handle_exception
  443.             self.raise_uncaught_exception(exc)

File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
  480.             response = handler(request, *args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
  62.             return bound_func(*args, **kwargs)

File "/opt/auth/decorators.py" in wrapped_view_function
  37.             return view_function(request, *args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/django/utils/decorators.py" in bound_func
  58.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/opt/api/resources/unit_features/views.py" in create
  66.         return super().create(request)

File "/usr/local/lib/python3.6/site-packages/drf_jsonapi/mixins.py" in create
  124.                 request.data["data"]["relationships"], resource, request, many=False

File "/usr/local/lib/python3.6/site-packages/drf_jsonapi/mixins.py" in process_relationships
  91.                 data, many=handler.many

File "/usr/local/lib/python3.6/site-packages/drf_jsonapi/serializers/resources.py" in from_identity
  378.             return cls.get_object_by_id(data["id"])

File "/usr/local/lib/python3.6/site-packages/drf_jsonapi/serializers/resources.py" in get_object_by_id
  461.             return cls.Meta.model.objects.get(**{id_field: identifier})

File "/usr/local/lib/python3.6/site-packages/django/db/models/manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py" in get
  394.         clone = self.filter(*args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py" in filter
  836.         return self._filter_or_exclude(False, *args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py" in _filter_or_exclude
  854.             clone.query.add_q(Q(*args, **kwargs))

File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py" in add_q
  1253.         clause, _ = self._add_q(q_object, self.used_aliases)

File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py" in _add_q
  1277.                     split_subq=split_subq,

File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py" in build_filter
  1215.         condition = self.build_lookup(lookups, col, value)

File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py" in build_lookup
  1085.         lookup = lookup_class(lhs, rhs)

File "/usr/local/lib/python3.6/site-packages/django/db/models/lookups.py" in __init__
  18.         self.rhs = self.get_prep_lookup()

File "/usr/local/lib/python3.6/site-packages/django/db/models/lookups.py" in get_prep_lookup
  68.             return self.lhs.output_field.get_prep_value(self.rhs)

File "/usr/local/lib/python3.6/site-packages/django/db/models/fields/__init__.py" in get_prep_value
  947.         return int(value)

Exception Type: ValueError at /unit-features
Exception Value: invalid literal for int() with base 10: 'asklfj'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant