Skip to content

[Control Plane] Extract shared ErrorResponse helper #119

@santoshkumarradha

Description

@santoshkumarradha

Summary

Multiple handler files define similar ErrorResponse structs and error handling patterns. Extract these to a shared location to reduce duplication.

Current State

  • Issue: ErrorResponse struct and similar error patterns repeated across 10+ handler files
  • Example location: control-plane/internal/handlers/memory.go (lines 52-57)

Current Pattern (Duplicated)

// Found in multiple files:
type ErrorResponse struct {
    Error string \`json:"error"\`
}

// Error response code duplicated:
c.JSON(http.StatusBadRequest, ErrorResponse{Error: "invalid request"})

Proposed Solution

Create a shared error utilities file:

// control-plane/internal/handlers/errors.go

package handlers

// ErrorResponse is the standard error response format.
type ErrorResponse struct {
    Error   string \`json:"error"\`
    Code    string \`json:"code,omitempty"\`
    Details string \`json:"details,omitempty"\`
}

// RespondError sends a JSON error response with the given status code.
func RespondError(c *gin.Context, status int, message string) {
    c.JSON(status, ErrorResponse{Error: message})
}

// RespondBadRequest sends a 400 Bad Request error response.
func RespondBadRequest(c *gin.Context, message string) {
    RespondError(c, http.StatusBadRequest, message)
}

// RespondNotFound sends a 404 Not Found error response.
func RespondNotFound(c *gin.Context, message string) {
    RespondError(c, http.StatusNotFound, message)
}

// RespondInternalError sends a 500 Internal Server Error response.
func RespondInternalError(c *gin.Context, message string) {
    RespondError(c, http.StatusInternalServerError, message)
}

Tasks

  1. Create control-plane/internal/handlers/errors.go with shared types/functions
  2. Update existing handlers to use shared error utilities
  3. Remove duplicate ErrorResponse definitions

Acceptance Criteria

  • Shared error file created
  • At least 3 handler files updated to use shared utilities
  • No duplicate ErrorResponse struct definitions remain
  • All tests pass
  • Code compiles without errors

Files

  • control-plane/internal/handlers/errors.go (new)
  • Multiple handler files to update

Using AI to solve this issue? Read our AI-Assisted Contributions guide for testing requirements, prompt strategies, and common pitfalls to avoid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:control-planeControl plane server functionalitygood first issueGood for newcomersrefactorCode quality and refactoring improvements

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions