Skip to content

Commit

Permalink
Fix Within an except clause, raise exceptions with `raise ... from …
Browse files Browse the repository at this point in the history
…err` or `raise ... from None` to distinguish them from errors in exception handling
  • Loading branch information
brianhelba committed Nov 16, 2023
1 parent 5beddfa commit f8855f2
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dandiapi/api/asset_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def insert_asset_paths(asset: Asset, version: Version):
# If there are simultaneous requests to create the same asset, this check constraint can
# fail, and should be handled directly, rather than be allowed to bubble up
if 'unique-version-path' in str(e):
raise AssetAlreadyExists
raise AssetAlreadyExists from e

# Re-raise original exception otherwise
raise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def migrate_published_version_metadata(dandiset: str, published_version: str, to
except Exception as e:
click.echo(f'Failed to migrate {dandiset}/{published_version}')
click.echo(e)
raise click.Abort
raise click.Abort from e

if metadata == metanew:
click.echo('No changes detected')
Expand Down
4 changes: 2 additions & 2 deletions dandiapi/api/views/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ def user_questionnaire_form_view(request: HttpRequest) -> HttpResponse:
try:
# questions to display in the form
questions = json.loads(request.GET.get('QUESTIONS'))
except (JSONDecodeError, TypeError):
raise Http404
except (JSONDecodeError, TypeError) as e:
raise Http404 from e

return render(
request,
Expand Down
8 changes: 4 additions & 4 deletions dandiapi/api/views/dandiset.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ def get_object(self):
lookup_url = self.kwargs[self.lookup_url_kwarg]
try:
lookup_value = int(lookup_url)
except ValueError:
raise Http404('Not a valid identifier.')
except ValueError as e:
raise Http404('Not a valid identifier.') from e
self.kwargs[self.lookup_url_kwarg] = lookup_value

dandiset = super().get_object()
Expand Down Expand Up @@ -383,8 +383,8 @@ def get_user_or_400(username):
else:
try:
return User.objects.get(username=username)
except ObjectDoesNotExist:
raise ValidationError(f'User {username} not found')
except ObjectDoesNotExist as e:
raise ValidationError(f'User {username} not found') from e

owners = [
get_user_or_400(username=owner['username']) for owner in serializer.validated_data
Expand Down
6 changes: 3 additions & 3 deletions dandiapi/api/views/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def upload_complete_view(request: Request, upload_id: str) -> HttpResponseBase:
except Upload.DoesNotExist:
upload = get_object_or_404(EmbargoedUpload, upload_id=upload_id)
if not request.user.has_perm('owner', upload.dandiset):
raise Http404
raise Http404 from None

completion = TransferredParts(
object_key=upload.blob.name,
Expand Down Expand Up @@ -237,7 +237,7 @@ def upload_validate_view(request: Request, upload_id: str) -> HttpResponseBase:
except Upload.DoesNotExist:
upload = get_object_or_404(EmbargoedUpload, upload_id=upload_id)
if not request.user.has_perm('owner', upload.dandiset):
raise Http404
raise Http404 from None

# Verify that the upload was successful
if not upload.object_key_exists():
Expand Down Expand Up @@ -275,7 +275,7 @@ def upload_validate_view(request: Request, upload_id: str) -> HttpResponseBase:
asset_blob = upload.to_asset_blob()
asset_blob.save()
else:
raise ValueError(f'Unknown Upload type {type(upload)}')
raise ValueError(f'Unknown Upload type {type(upload)}') from None

# Clean up the upload
upload.delete()
Expand Down
4 changes: 2 additions & 2 deletions dandiapi/zarr/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ def create(self, request):
zarr_archive: ZarrArchive = ZarrArchive(name=name, dandiset=dandiset)
try:
zarr_archive.save()
except IntegrityError:
raise ValidationError('Zarr already exists')
except IntegrityError as e:
raise ValidationError('Zarr already exists') from e

serializer = ZarrSerializer(instance=zarr_archive)
return Response(serializer.data, status=status.HTTP_200_OK)
Expand Down

0 comments on commit f8855f2

Please sign in to comment.