Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): use ValidationError with a field speficied when possible #13541

Merged
merged 1 commit into from
Jan 16, 2025

Conversation

nijel
Copy link
Member

@nijel nijel commented Jan 16, 2025

Proposed changes

  • include field when it is known
  • avoid building 400 responses manually

Checklist

  • Lint and unit tests pass locally with my changes.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added documentation to describe my feature.
  • I have squashed my commits into logic units.
  • I have described the changes in the commit messages.

Other information

@nijel nijel added this to the 5.10 milestone Jan 16, 2025
@nijel nijel self-assigned this Jan 16, 2025
@nijel nijel requested a review from orangesunny as a code owner January 16, 2025 13:52
@nijel nijel enabled auto-merge (rebase) January 16, 2025 13:52

try:
group = Group.objects.get(pk=int(request.data["group_id"]))
except (Group.DoesNotExist, ValueError) as error:
raise ValidationError(str(error)) from error
raise ValidationError({"group_id": str(error)}) from error

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.

try:
role = Role.objects.get(pk=int(request.data["role_id"]))
except (Role.DoesNotExist, ValueError) as error:
raise ValidationError(str(error)) from error
raise ValidationError({"role_id": str(error)}) from error

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.

try:
language = Language.objects.get(code=request.data["language_code"])
except (Language.DoesNotExist, ValueError) as error:
raise ValidationError(str(error)) from error
raise ValidationError({"language_code": str(error)}) from error

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.

try:
project = Project.objects.get(
pk=int(request.data["project_id"]),
)
except (Project.DoesNotExist, ValueError) as error:
raise ValidationError(str(error)) from error
raise ValidationError({"project_id": str(error)}) from error

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.

try:
component_list = ComponentList.objects.get(
pk=int(request.data["component_list_id"]),
)
except (ComponentList.DoesNotExist, ValueError) as error:
raise ValidationError(str(error)) from error
raise ValidationError({"component_list_id": str(error)}) from error

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.

try:
component = Component.objects.filter_access(request.user).get(
pk=int(request.data["component_id"])
)
except (Component.DoesNotExist, ValueError) as error:
raise ValidationError(str(error)) from error
raise ValidationError({"component_id": str(error)}) from error

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
weblate/api/views.py Fixed Show fixed Hide fixed
weblate/api/views.py Fixed Show fixed Hide fixed
@@ -1679,7 +1673,7 @@
parse_query(query_string)
except Exception as error:
msg = f"Could not parse query string: {error}"
raise ValidationError(msg) from error
raise ValidationError({"q": msg}) from error

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
@@ -1821,7 +1815,7 @@
parse_query(query_string)
except Exception as error:
msg = f"Could not parse query string: {error}"
raise ValidationError(msg) from error
raise ValidationError({"q": msg}) from error

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
@nijel nijel force-pushed the api-validation branch 2 times, most recently from ede4cdc to f0fb309 Compare January 16, 2025 14:17
- include field when it is known
- avoid building 400 responses manually
nijel added a commit to nijel/weblate that referenced this pull request Jan 16, 2025
This is companion to WeblateOrg#13541 to make API error response more consistent.
We now rely on drf-standardized-erros to do the processing. It also has
drf-spectacular integration, so the error states are now documented in
OpenAPI.

service, configuration, errors = validate_service_configuration(
service_name, request.data.get("configuration", "{}")
)

if service is None or errors:
return Response({"errors": errors}, status=HTTP_400_BAD_REQUEST)
raise ValidationError({"configuration": errors})

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
@@ -1199,7 +1193,7 @@
)

if service is None or errors:
return Response({"errors": errors}, status=HTTP_400_BAD_REQUEST)
raise ValidationError({"configuration": errors})

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
@nijel nijel merged commit 7738303 into WeblateOrg:main Jan 16, 2025
34 of 46 checks passed
@nijel nijel deleted the api-validation branch January 16, 2025 17:05
nijel added a commit to nijel/weblate that referenced this pull request Jan 16, 2025
This is companion to WeblateOrg#13541 to make API error response more consistent.
We now rely on drf-standardized-erros to do the processing. It also has
drf-spectacular integration, so the error states are now documented in
OpenAPI.
nijel added a commit to nijel/weblate that referenced this pull request Jan 16, 2025
This is companion to WeblateOrg#13541 to make API error response more consistent.
We now rely on drf-standardized-erros to do the processing. It also has
drf-spectacular integration, so the error states are now documented in
OpenAPI.
nijel added a commit to nijel/weblate that referenced this pull request Jan 16, 2025
This is companion to WeblateOrg#13541 to make API error response more consistent.
We now rely on drf-standardized-erros to do the processing. It also has
drf-spectacular integration, so the error states are now documented in
OpenAPI.
Copy link

codecov bot commented Jan 16, 2025

Codecov Report

Attention: Patch coverage is 62.96296% with 10 lines in your changes missing coverage. Please review.

Project coverage is 90.95%. Comparing base (48bf8dc) to head (26c02cd).
Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
weblate/api/views.py 62.96% 10 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #13541   +/-   ##
=======================================
  Coverage   90.95%   90.95%           
=======================================
  Files         604      604           
  Lines       62823    62823           
  Branches     6488     6488           
=======================================
  Hits        57140    57140           
  Misses       3999     3999           
  Partials     1684     1684           
Files with missing lines Coverage Δ
weblate/api/views.py 89.66% <62.96%> (ø)

... and 1 file with indirect coverage changes

nijel added a commit to nijel/weblate that referenced this pull request Jan 16, 2025
This is companion to WeblateOrg#13541 to make API error response more consistent.
We now rely on drf-standardized-erros to do the processing. It also has
drf-spectacular integration, so the error states are now documented in
OpenAPI.
nijel added a commit to nijel/weblate that referenced this pull request Jan 17, 2025
This is companion to WeblateOrg#13541 to make API error response more consistent.
We now rely on drf-standardized-erros to do the processing. It also has
drf-spectacular integration, so the error states are now documented in
OpenAPI.
nijel added a commit to nijel/weblate that referenced this pull request Jan 17, 2025
This is companion to WeblateOrg#13541 to make API error response more consistent.
We now rely on drf-standardized-erros to do the processing. It also has
drf-spectacular integration, so the error states are now documented in
OpenAPI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant