feat(api): align response envelope schema

Switch response envelope business code to numeric and make message
consistently present. Add trace_id and optional details, and remove the
duplicate DTO envelope definition. Improve middleware path exclusion
handling and add a time-based trace ID fallback if crypto RNG fails.

BREAKING CHANGE: response envelope `code` is now `int` (was `string`) and
`message` semantics/defaults changed; clients must update parsing.
This commit is contained in:
zenfun
2026-01-10 01:09:05 +08:00
parent 26733be020
commit ac6a1858cf
3 changed files with 20 additions and 66 deletions

View File

@@ -1,60 +0,0 @@
package dto
// ResponseEnvelope is the standard API response wrapper.
// All JSON API responses are wrapped in this envelope format.
// @Description Standard API response envelope
type ResponseEnvelope struct {
// Business code: 0 for success, non-zero for errors
// 0 = success
// 1xxx = common errors (invalid param, unauthorized, forbidden, rate limited)
// 4xxx = client errors (resource not found, conflict, invalid state)
// 5xxx = server errors (internal error, service unavailable, timeout)
Code int `json:"code" example:"0"`
// Human-readable message: "success" for success, error description for errors
Message string `json:"message" example:"success"`
// Response data: business payload for success, null for errors
Data any `json:"data"`
// Request trace ID for debugging and log correlation
TraceID string `json:"trace_id" example:"a1b2c3d4e5f6g7h8"`
// Optional error details (e.g., field validation errors)
Details any `json:"details,omitempty"`
}
// ErrorResponse is returned when an error occurs.
// @Description Error response format
type ErrorResponse struct {
// Business error code (non-zero)
Code int `json:"code" example:"4001"`
// Error message
Message string `json:"message" example:"resource not found"`
// Always null for errors
Data any `json:"data" example:"null"`
// Request trace ID
TraceID string `json:"trace_id" example:"a1b2c3d4e5f6g7h8"`
// Optional structured error details
Details any `json:"details,omitempty"`
}
// SuccessResponse is returned for successful operations.
// @Description Success response format
type SuccessResponse struct {
// Always 0 for success
Code int `json:"code" example:"0"`
// Always "success"
Message string `json:"message" example:"success"`
// Response payload
Data any `json:"data"`
// Request trace ID
TraceID string `json:"trace_id" example:"a1b2c3d4e5f6g7h8"`
}