Commit a37166c3 by jsa Committed by Andy Armstrong

remove catchall exception handling in profile image api

parent bb2599ce
...@@ -223,12 +223,8 @@ class ProfileImageUploadTestCase(ProfileImageEndpointTestCase): ...@@ -223,12 +223,8 @@ class ProfileImageUploadTestCase(ProfileImageEndpointTestCase):
""" """
image_open.side_effect = [Exception(u"whoops"), None] image_open.side_effect = [Exception(u"whoops"), None]
with make_image_file() as image_file: with make_image_file() as image_file:
response = self.client.post(self.url, {'file': image_file}, format='multipart') with self.assertRaises(Exception):
self.check_response( self.client.post(self.url, {'file': image_file}, format='multipart')
response, 400,
expected_developer_message=u"Upload failed for profile image: whoops",
expected_user_message=u"Upload failed for profile image",
)
self.check_images(False) self.check_images(False)
self.check_has_profile_image(False) self.check_has_profile_image(False)
self.assertFalse(mock_log.info.called) self.assertFalse(mock_log.info.called)
...@@ -321,12 +317,8 @@ class ProfileImageRemoveTestCase(ProfileImageEndpointTestCase): ...@@ -321,12 +317,8 @@ class ProfileImageRemoveTestCase(ProfileImageEndpointTestCase):
messages are returned. messages are returned.
""" """
user_profile_save.side_effect = [Exception(u"whoops"), None] user_profile_save.side_effect = [Exception(u"whoops"), None]
response = self.client.post(self.url) with self.assertRaises(Exception):
self.check_response( self.client.post(self.url)
response, 400,
expected_developer_message=u"Delete failed for profile image: whoops",
expected_user_message=u"Delete failed for profile image",
)
self.check_images(True) # thumbnails should remain intact. self.check_images(True) # thumbnails should remain intact.
self.check_has_profile_image(True) self.check_has_profile_image(True)
self.assertFalse(mock_log.info.called) self.assertFalse(mock_log.info.called)
...@@ -80,41 +80,31 @@ class ProfileImageUploadView(APIView): ...@@ -80,41 +80,31 @@ class ProfileImageUploadView(APIView):
status=status.HTTP_400_BAD_REQUEST status=status.HTTP_400_BAD_REQUEST
) )
try: # process the upload.
# process the upload. uploaded_file = request.FILES['file']
uploaded_file = request.FILES['file']
# no matter what happens, delete the temporary file when we're done
# no matter what happens, delete the temporary file when we're done with closing(uploaded_file):
with closing(uploaded_file):
# image file validation.
# image file validation. try:
try: validate_uploaded_image(uploaded_file)
validate_uploaded_image(uploaded_file) except ImageValidationError as error:
except ImageValidationError as error: return Response(
return Response( {"developer_message": error.message, "user_message": error.user_message},
{"developer_message": error.message, "user_message": error.user_message}, status=status.HTTP_400_BAD_REQUEST,
status=status.HTTP_400_BAD_REQUEST,
)
# generate profile pic and thumbnails and store them
profile_image_names = get_profile_image_names(username)
create_profile_images(uploaded_file, profile_image_names)
# update the user account to reflect that a profile image is available.
set_has_profile_image(username, True, _make_upload_dt())
log.info(
LOG_MESSAGE_CREATE,
{'image_names': profile_image_names.values(), 'user_id': request.user.id}
) )
except Exception as error:
return Response(
{
"developer_message": u"Upload failed for profile image: {error}".format(error=error),
"user_message": _(u"Upload failed for profile image"),
}, # generate profile pic and thumbnails and store them
status=status.HTTP_400_BAD_REQUEST profile_image_names = get_profile_image_names(username)
create_profile_images(uploaded_file, profile_image_names)
# update the user account to reflect that a profile image is available.
set_has_profile_image(username, True, _make_upload_dt())
log.info(
LOG_MESSAGE_CREATE,
{'image_names': profile_image_names.values(), 'user_id': request.user.id}
) )
# send client response. # send client response.
...@@ -165,15 +155,6 @@ class ProfileImageRemoveView(APIView): ...@@ -165,15 +155,6 @@ class ProfileImageRemoveView(APIView):
) )
except UserNotFound: except UserNotFound:
return Response(status=status.HTTP_404_NOT_FOUND) return Response(status=status.HTTP_404_NOT_FOUND)
except Exception as error:
return Response(
{
"developer_message": u"Delete failed for profile image: {error}".format(error=error),
"user_message": _(u"Delete failed for profile image"),
},
status=status.HTTP_400_BAD_REQUEST
)
# send client response. # send client response.
return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_204_NO_CONTENT)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment