Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
Handle 400 and 404 errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémy HUBSCHER committed Sep 3, 2015
1 parent 6109ddc commit 43a56bd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 0 additions & 2 deletions syncto/authentication.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from pyramid import httpexceptions
from pyramid.security import forget
from requests.exceptions import HTTPError

from cliquet.errors import http_error, ERRORS
from cliquet.views.errors import service_unavailable
from sync.client import SyncClient

from syncto import AUTHORIZATION_HEADER, CLIENT_STATE_HEADER
Expand Down
7 changes: 7 additions & 0 deletions syncto/tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,10 @@ def test_delete_return_a_503_in_case_of_error(self):
self.sync_client.return_value.delete_record.side_effect = HTTPError(
response=response)
self.app.delete(RECORD_URL, headers=self.headers, status=503)

def test_delete_return_a_404_in_case_of_unknown_ressource(self):
response = mock.MagicMock()
response.status_code = 404
self.sync_client.return_value.delete_record.side_effect = HTTPError(
response=response)
self.app.delete(RECORD_URL, headers=self.headers, status=404)
26 changes: 17 additions & 9 deletions syncto/views/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@ def error(context, request):
"""Catch server errors and trace them."""
logger.error(context, exc_info=True)

if context.response.status_code in (400, 401):
message = '%s %s: %s' % (context.response.status_code,
context.response.reason,
context.response.text)
response = http_error(httpexceptions.HTTPUnauthorized(),
errno=ERRORS.INVALID_AUTH_TOKEN,
message=message)
# Forget the current user credentials.
response.headers.extend(forget(request))
message = '%s %s: %s' % (context.response.status_code,
context.response.reason,
context.response.text)
if context.response.status_code == 400:
response = http_error(httpexceptions.HTTPBadRequest(),
errno=ERRORS.INVALID_PARAMETERS,
message=message)
elif context.response.status_code == 401:
response = http_error(httpexceptions.HTTPUnauthorized(),
errno=ERRORS.INVALID_AUTH_TOKEN,
message=message)
# Forget the current user credentials.
response.headers.extend(forget(request))
elif context.response.status_code == 404:
response = http_error(httpexceptions.HTTPNotFound(),
errno=ERRORS.INVALID_RESOURCE_ID,
message=message)
else:
response = service_unavailable(context, request)

Expand Down

0 comments on commit 43a56bd

Please sign in to comment.