package response import ( "net/http" "github.com/gin-gonic/gin" "go.opentelemetry.io/otel/trace" "switchbot-api/internal/transport/http/contextkeys" ) type ErrorBody struct { Error string `json:"error"` TraceID string `json:"trace_id,omitempty"` RequestID string `json:"request_id,omitempty"` } func WriteError(c *gin.Context, status int, message string) { spanCtx := trace.SpanContextFromContext(c.Request.Context()) body := ErrorBody{Error: message} if spanCtx.IsValid() { body.TraceID = spanCtx.TraceID().String() } if requestID := contextkeys.RequestIDFromContext(c.Request.Context()); requestID != "" { body.RequestID = requestID } c.JSON(status, body) } func WriteInternalError(c *gin.Context) { WriteError(c, http.StatusInternalServerError, "internal server error") }