package cdata import ( "errors" "git.blauwelle.com/go/crate/cerrors" "git.blauwelle.com/go/crate/runtimehelper" ) const maximumFrames = 16 type Response[T any] struct { Data T `json:"data,omitempty"` Code cerrors.Code `json:"code"` Message string `json:"message,omitempty"` Debug string `json:"debug,omitempty"` Traceback []runtimehelper.Frame `json:"traceback,omitempty"` } type PageData[T any] struct { List []T `json:"list"` PageIndex int `json:"pageIndex"` // >=1 PageSize int `json:"pageSize"` // >=1 Total int `json:"total"` // maybe 0 } func NewOkResponse[T any](data T) Response[T] { return Response[T]{ Code: cerrors.CodeOK, Data: data, } } func NewResponseFromError(err error) Response[struct{}] { e := &cerrors.Error{} if !errors.As(err, &e) { response := Response[struct{}]{ Code: cerrors.CodeUnknown, Message: "未知错误", } if cerrors.FlagDebug { response.Debug = err.Error() response.Traceback = runtimehelper.Stack(1, maximumFrames) } return response } return Response[struct{}]{ Code: e.Code, Message: e.Message, Debug: e.Debug, Traceback: e.Traceback, } } func NewErrorResponse(code cerrors.Code, message string) Response[struct{}] { response := Response[struct{}]{ Code: code, Message: message, } if cerrors.FlagDebug { response.Traceback = runtimehelper.Stack(1, maximumFrames) } return response }