Skip to content

TimelinesController API Responses #487

@dh4340

Description

@dh4340

Issue: TimelinesController API Responses

Description

The TimelinesController has several issues related to API response consistency, error handling, and best practices. This issue highlights the key problems and suggests improvements.


Problems Identified

1. Inconsistent HTTP Responses

  • The API returns 204 No Content for operations that modify data (e.g., creating/updating a timeline or stopping a timeline).
  • A 204 status code does not return a response body, making it difficult for API clients to confirm success.
  • Recommended Approach: Return structured success responses with a 200 OK status, including a message indicating the operation's success.

2. Lack of Detailed Error Messages

  • If a requested machine or timeline does not exist, the original code only returns 404 Not Found without a meaningful message.
  • Recommended Approach: Return a structured error response with a message field explaining why the request failed.

3. Missing BadRequest Handling

  • The POST request for creating/updating a timeline does not validate the request body, potentially leading to unhandled exceptions.
  • Recommended Approach: Add a BadRequest response (400) when the input is invalid or missing required fields.

4. Improper Exception Handling in StopTimeline

  • The StopTimeline method does not handle specific exceptions properly. If a timeline is not found, an unhandled exception might be thrown.
  • Recommended Approach: Use structured exception handling to return 404 Not Found for missing timelines and 400 Bad Request for general errors.

Proposed Fixes

The following improvements would significantly enhance the TimelinesController:

Consistent Success Responses

  • All modification operations (POST, STOP) should return 200 OK with a JSON response:
    {
      "success": true,
      "message": "Timeline updated successfully"
    }

Improved Error Handling

  • 404 Not Found should include an error message:
    {
      "success": false,
      "message": "No timelines found for Machine ID: {machineId}"
    }
  • 400 Bad Request should be returned when inputs are invalid.

Better Exception Handling in StopTimeline

  • Modify the method so that it catches KeyNotFoundException and returns a 404 response instead of an unhandled error.

Metadata

Metadata

Assignees

No one assigned

    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