Skip to content

Refactor: Align Create Task Endpoint with Global Exception Handling #92

@Achintya-Chatterjee

Description

@Achintya-Chatterjee

Issue Description

The "create task" endpoint handles exceptions locally within the view and service layers, which is inconsistent with the rest of the application, where exceptions are managed by a global exception handler (exception_handler.py). This refactoring task aims to align the create task endpoint's error handling with the project's standard pattern.

PRD: https://docs.google.com/document/d/1KOFK8ISE1hy9h8QjNbrObo6QKeM1I-rYZTJ8ygwfxhg/edit?usp=sharing

Expected Behavior

  • When a POST request to /tasks results in an error (e.g., validation error, repository error), the exception should be propagated up to the global exception handler.
  • The global exception handler (todo/exceptions/exception_handler.py) should be responsible for formatting and returning the final error response to the client.
  • The local try...except blocks in TaskListView.post and TaskService.create_task that generate ApiErrorResponse objects should be removed.
  • The service layer should raise specific, custom exceptions (e.g., DRFValidationError for validation issues) or let repository/database exceptions propagate, which the global handler will then catch.

Current Behavior

  • The TaskListView.post method in todo/views/task.py contains a try...except block that catches ValueError and manually constructs an ApiErrorResponse.
  • The TaskService.create_task method in todo/services/task_service.py also has try...except blocks. It catches exceptions, wraps them in an ApiErrorResponse, and re-raises them as a generic ValueError.
  • This local handling prevents the global exception handler from processing errors for this endpoint, leading to inconsistent error response formats and duplicated logic. For example, validation errors for missing labels are wrapped in a ValueError with an ApiErrorResponse instead of raising a DRFValidationError that the global handler can process correctly.

Screenshots

N/A

Reproducibility

  • This issue is reproducible
  • This issue is not reproducible

Steps to Reproduce

  1. Send a `POST` request to the `/tasks` endpoint with a non-existent `label` ID in the payload.
  2. Set a breakpoint in `todo/views/task.py` inside the `except ValueError` block of the `post` method.
  3. Observe that the exception is caught and handled locally, instead of being processed by the global exception handler in `todo/exceptions/exception_handler.py`.
  4. Compare this behaviour with error handling in other endpoints, like `TaskDetailView`, which relies on the global handler.

Severity/Priority

  • Critical
  • High
  • Medium
  • Low

Additional Information

This issue primarily involves refactoring the following methods:

  • todo.views.task.TaskListView.post
  • todo.services.task_service.TaskService.create_task

The goal is to make their exception handling similar to TaskDetailView and other parts of the service layer.

Checklist

  • I have read and followed the project's code of conduct.
  • I have searched for similar issues before creating this one.
  • I have provided all the necessary information to understand and reproduce the issue.
  • I am willing to contribute to the resolution of this issue.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions