You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
crate/httpdata/data.go

52 lines
1.1 KiB
Go

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