Skip to content

Commit

Permalink
Merge pull request #15 from CodeTeam/return_error
Browse files Browse the repository at this point in the history
add detail in error messages
  • Loading branch information
mvshalamov committed Nov 28, 2016
2 parents 2a4cd11 + caeda4c commit 77c6640
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 71 deletions.
2 changes: 1 addition & 1 deletion tcrudge/__init__.py
Expand Up @@ -6,5 +6,5 @@
Supports JSON and MessagePack responses.
"""

__version__ = "0.6.3"
__version__ = "0.6.4"

219 changes: 149 additions & 70 deletions tcrudge/handlers.py
Expand Up @@ -117,12 +117,18 @@ def validate(self, data, schema):
else:
_data = json.loads(data.decode())
validate(_data, schema)
except ValueError:
except ValueError as e:
# json.loads error
raise web.HTTPError(400, reason=self.get_response(
errors=[{'code': '',
'message': 'Request body is not a valid json '
'object'}]))
errors=[
{
'code': '',
'message': 'Request body is not a valid json object',
'detail': str(e)
}
]
)
)
except exceptions.ValidationError as exc:
# data does not pass validation
raise web.HTTPError(400, reason=self.get_response(
Expand All @@ -137,9 +143,15 @@ async def bad_permissions(self):
:raises: HTTPError 401
"""
raise web.HTTPError(
401,
reason=self.get_response(
errors=[{'code': '', 'message': 'Access denied'}])
401,
reason=self.get_response(
errors=[
{
'code': '',
'message': 'Access denied'
}
]
)
)

async def is_auth(self):
Expand Down Expand Up @@ -561,14 +573,20 @@ async def _get_items(self, qs):
*self.prefetch_queries)
else:
items = await self.application.objects.execute(qs)
except (peewee.DataError, ValueError):
except (peewee.DataError, ValueError) as e:
# Bad parameters
raise web.HTTPError(400,
reason=self.get_response(errors=[{'code': '',
'message':
'Bad '
'query '
'arguments'}]))
raise web.HTTPError(
400,
reason=self.get_response(
errors=[
{
'code': '',
'message':'Bad query arguments',
'detail': str(e)
}
]
)
)
# Set number of fetched items
pagination['limit'] = len(items) # TODO WTF? Why limit is set?

Expand All @@ -591,14 +609,20 @@ async def get(self):
self.get_schema_input)
try:
qs = self.get_queryset()
except AttributeError:
except AttributeError as e:
# Wrong field name in filter or order_by
raise web.HTTPError(400,
reason=self.get_response(errors=[{'code': '',
'message':
'Bad '
'query '
'arguments'}]))
raise web.HTTPError(
400,
reason=self.get_response(
errors=[
{
'code': '',
'message':'Bad query arguments',
'detail': str(e)
}
]
)
)
items, pagination = await self._get_items(qs)
result = []
for m in items:
Expand All @@ -620,24 +644,36 @@ async def head(self):
self.get_schema_input)
try:
qs = self.get_queryset(paginate=False)
except AttributeError:
except AttributeError as e:
# Wrong field name in filter or order_by
raise web.HTTPError(400,
reason=self.get_response(errors=[{'code': '',
'message':
'Bad '
'query '
'arguments'}]))
raise web.HTTPError(
400,
reason=self.get_response(
errors=[
{
'code': '',
'message':'Bad query arguments',
'detail': str(e)
}
]
)
)
try:
total_num = await self.application.objects.count(qs)
except (peewee.DataError, peewee.ProgrammingError, ValueError):
except (peewee.DataError, peewee.ProgrammingError, ValueError) as e:
# Bad parameters
raise web.HTTPError(400,
reason=self.get_response(errors=[{'code': '',
'message':
'Bad '
'query '
'arguments'}]))
raise web.HTTPError(
400,
reason=self.get_response(
errors=[
{
'code': '',
'message':'Bad query arguments',
'detail': str(e)
}
]
)
)
self.set_header('X-Total', total_num)
self.finish()

Expand All @@ -658,19 +694,33 @@ async def post(self):
data = self.validate(self.request.body, self.post_schema_input)
try:
item = await self.model_cls._create(self.application, data)
except AttributeError:
except AttributeError as e:
# We can only create item if _create() model method implemented
raise web.HTTPError(405,
reason=self.get_response(errors=[{'code': '',
'message':
'Method '
'not '
'allowed'}]))
except (peewee.IntegrityError, peewee.DataError):
raise web.HTTPError(400,
reason=self.get_response(errors=[{'code': '',
'message':
'Invalid parameters'}]))
raise web.HTTPError(
405,
reason=self.get_response(
errors=[
{
'code': '',
'message':'Method not allowed',
'detail': str(e)
}
]
)
)
except (peewee.IntegrityError, peewee.DataError) as e:
raise web.HTTPError(
400,
reason=self.get_response(
errors=[
{
'code': '',
'message':'Invalid parameters',
'detail': str(e)
}
]
)
)
self.response(result=await self.serialize(item))


Expand Down Expand Up @@ -748,10 +798,19 @@ async def get_item(self, item_id):
return await self.application.objects.get(self.model_cls,
**{
self.model_cls._meta.primary_key.name: item_id})
except (self.model_cls.DoesNotExist, ValueError):
raise web.HTTPError(404,
reason=self.get_response(errors=[
{'code': '', 'message': 'Item not found'}]))
except (self.model_cls.DoesNotExist, ValueError) as e:
raise web.HTTPError(
404,
reason=self.get_response(
errors=[
{
'code': '',
'message': 'Item not found',
'detail': str(e)
}
]
)
)

async def get(self, item_id):
"""
Expand Down Expand Up @@ -787,19 +846,33 @@ async def put(self, item_id):
data = self.validate(self.request.body, self.put_schema_input)
try:
item = await item._update(self.application, data)
except AttributeError:
except AttributeError as e:
# We can only update item if model method _update is implemented
raise web.HTTPError(405,
reason=self.get_response(errors=[{'code': '',
'message':
'Method '
'not '
'allowed'}]))
except (peewee.IntegrityError, peewee.DataError):
raise web.HTTPError(400,
reason=self.get_response(errors=[{'code': '',
'message':
'Invalid parameters'}]))
raise web.HTTPError(
405,
reason=self.get_response(
errors=[
{
'code': '',
'message':'Method not allowed',
'detail': str(e)
}
]
)
)
except (peewee.IntegrityError, peewee.DataError) as e:
raise web.HTTPError(
400,
reason=self.get_response(
errors=[
{
'code': '',
'message':'Invalid parameters',
'detail': str(e)
}
]
)
)

self.response(result=await self.serialize(item))

Expand All @@ -821,12 +894,18 @@ async def delete(self, item_id):
try:
# We can only delete item if model method _delete() is implemented
await item._delete(self.application)
except AttributeError:
raise web.HTTPError(405,
reason=self.get_response(errors=[{'code': '',
'message':
'Method '
'not '
'allowed'}]))
except AttributeError as e:
raise web.HTTPError(
405,
reason=self.get_response(
errors=[
{
'code': '',
'message':'Method not allowed',
'detail': str(e)
}
]
)
)

self.response(result='Item deleted')

0 comments on commit 77c6640

Please sign in to comment.