Skip to content

Commit

Permalink
better error handling when object is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
akira-dev committed Mar 31, 2017
1 parent ee74ebd commit 515d71a
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion flask_rest_jsonapi/data_layers/alchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from flask_rest_jsonapi.constants import DEFAULT_PAGE_SIZE
from flask_rest_jsonapi.data_layers.base import BaseDataLayer
from flask_rest_jsonapi.exceptions import RelationNotFound, RelatedObjectNotFound, JsonApiException,\
InvalidSort
InvalidSort, ObjectNotFound
from flask_rest_jsonapi.data_layers.filtering.alchemy import create_filters
from flask_rest_jsonapi.schema import get_relationships

Expand Down Expand Up @@ -110,6 +110,12 @@ def update_object(self, obj, data, view_kwargs):
:param dict view_kwargs: kwargs from the resource view
:return boolean: True if object have changed else False
"""
if obj is None:
url_field = getattr(self, 'url_field', 'id')
filter_value = view_kwargs[url_field]
raise ObjectNotFound({'parameter': url_field},
'{}: {} not found'.format(self.model.__class__.__name__, filter_value))

self.before_update_object(obj, data, view_kwargs)

relationship_fields = get_relationships(self.resource.schema)
Expand All @@ -133,6 +139,12 @@ def delete_object(self, obj, view_kwargs):
:param DeclarativeMeta item: an item from sqlalchemy
:param dict view_kwargs: kwargs from the resource view
"""
if obj is None:
url_field = getattr(self, 'url_field', 'id')
filter_value = view_kwargs[url_field]
raise ObjectNotFound({'parameter': url_field},
'{}: {} not found'.format(self.model.__class__.__name__, filter_value))

self.before_delete_object(obj, view_kwargs)

self.session.delete(obj)
Expand All @@ -157,6 +169,12 @@ def create_relationship(self, json_data, relationship_field, related_id_field, v

obj = self.get_object(view_kwargs)

if obj is None:
url_field = getattr(self, 'url_field', 'id')
filter_value = view_kwargs[url_field]
raise ObjectNotFound({'parameter': url_field},
'{}: {} not found'.format(self.model.__class__.__name__, filter_value))

if not hasattr(obj, relationship_field):
raise RelationNotFound('', "{} has no attribute {}".format(obj.__class__.__name__, relationship_field))

Expand Down Expand Up @@ -207,6 +225,12 @@ def get_relationship(self, relationship_field, related_type_, related_id_field,

obj = self.get_object(view_kwargs)

if obj is None:
url_field = getattr(self, 'url_field', 'id')
filter_value = view_kwargs[url_field]
raise ObjectNotFound({'parameter': url_field},
'{}: {} not found'.format(self.model.__class__.__name__, filter_value))

if not hasattr(obj, relationship_field):
raise RelationNotFound('', "{} has no attribute {}".format(obj.__class__.__name__, relationship_field))

Expand Down Expand Up @@ -237,6 +261,12 @@ def update_relationship(self, json_data, relationship_field, related_id_field, v

obj = self.get_object(view_kwargs)

if obj is None:
url_field = getattr(self, 'url_field', 'id')
filter_value = view_kwargs[url_field]
raise ObjectNotFound({'parameter': url_field},
'{}: {} not found'.format(self.model.__class__.__name__, filter_value))

if not hasattr(obj, relationship_field):
raise RelationNotFound('', "{} has no attribute {}".format(obj.__class__.__name__, relationship_field))

Expand Down Expand Up @@ -290,6 +320,12 @@ def delete_relationship(self, json_data, relationship_field, related_id_field, v

obj = self.get_object(view_kwargs)

if obj is None:
url_field = getattr(self, 'url_field', 'id')
filter_value = view_kwargs[url_field]
raise ObjectNotFound({'parameter': url_field},
'{}: {} not found'.format(self.model.__class__.__name__, filter_value))

if not hasattr(obj, relationship_field):
raise RelationNotFound('', "{} has no attribute {}".format(obj.__class__.__name__, relationship_field))

Expand Down

0 comments on commit 515d71a

Please sign in to comment.