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
Steps to Reproduce
-
Send a `POST` request to the `/tasks` endpoint with a non-existent `label` ID in the payload.
-
Set a breakpoint in `todo/views/task.py` inside the `except ValueError` block of the `post` method.
-
Observe that the exception is caught and handled locally, instead of being processed by the global exception handler in `todo/exceptions/exception_handler.py`.
-
Compare this behaviour with error handling in other endpoints, like `TaskDetailView`, which relies on the global handler.
Severity/Priority
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
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
/tasksresults in an error (e.g., validation error, repository error), the exception should be propagated up to the global exception handler.todo/exceptions/exception_handler.py) should be responsible for formatting and returning the final error response to the client.try...exceptblocks inTaskListView.postandTaskService.create_taskthat generate ApiErrorResponse objects should be removed.DRFValidationErrorfor validation issues) or let repository/database exceptions propagate, which the global handler will then catch.Current Behavior
TaskListView.postmethod intodo/views/task.pycontains atry...exceptblock that catchesValueErrorand manually constructs anApiErrorResponse.TaskService.create_taskmethod intodo/services/task_service.pyalso hastry...exceptblocks. It catches exceptions, wraps them in anApiErrorResponse, and re-raises them as a genericValueError.ValueErrorwith anApiErrorResponseinstead of raising aDRFValidationErrorthat the global handler can process correctly.Screenshots
N/A
Reproducibility
Steps to Reproduce
Severity/Priority
Additional Information
This issue primarily involves refactoring the following methods:
todo.views.task.TaskListView.posttodo.services.task_service.TaskService.create_taskThe goal is to make their exception handling similar to
TaskDetailViewand other parts of the service layer.Checklist