httpdata/add stacktrace;

develop httpdata/v0.0.2
Ge Song 9 months ago
parent 5cf4430bc8
commit 8671f2826d

@ -11,4 +11,5 @@ const (
CodeUnexpect Code = "unexpect"
CodeUnexpectPanic Code = "unexpect.panic"
CodeUnknown Code = "unknown"
CodeUnauthorized Code = "unauthorized"
)

@ -1,16 +1,24 @@
package httpdata
import (
"errors"
"git.blauwelle.com/go/crate/runtimehelper"
)
type Response struct {
Data any `json:"data,omitempty"`
Code Code `json:"code"`
Message string `json:"message,omitempty"`
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"`
}
type PageData struct {
List []any `json:"list"`
PageIndex int `json:"pageIndex"` // >=1
PageSize int `json:"pageSize"` // >=1
Total int `json:"total"` // maybe 0
List any `json:"list"`
PageIndex int64 `json:"pageIndex"` // >=1
PageSize int64 `json:"pageSize"` // >=1
Total int64 `json:"total"` // maybe 0
}
func NewOkResponse(data any) Response {
@ -20,3 +28,24 @@ func NewOkResponse(data any) Response {
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,
}
}

@ -2,11 +2,24 @@ package httpdata
import (
"errors"
"git.blauwelle.com/go/crate/runtimehelper"
)
const maximumFrames = 8
type UniverseError struct {
Code Code
Message string
Code Code
Message string
Debug string
Traceback []runtimehelper.Frame
}
func (err UniverseError) Error() string {
if err.Message == "" {
return string(err.Code)
}
return string(err.Code) + ": " + err.Message
}
func NewUniverseError(code Code, message string) error {
@ -23,13 +36,6 @@ func NewBadRequestError(message string) error {
}
}
func (err UniverseError) Error() string {
if err.Message == "" {
return string(err.Code)
}
return string(err.Code) + ": " + err.Message
}
func IsUniverseError(err error) bool {
return errors.As(err, &UniverseError{})
}

@ -0,0 +1,7 @@
package httpdata
import (
"os"
)
var FlagDebug = os.Getenv("DEBUG") == "true"

@ -1,3 +1,5 @@
module git.blauwelle.com/go/crate/httpdata
go 1.21.1
go 1.21
require git.blauwelle.com/go/crate/runtimehelper v0.2.0

@ -0,0 +1,2 @@
git.blauwelle.com/go/crate/runtimehelper v0.2.0 h1:W19wipPCyFSGHOWzqtfouNJu7MDeJobP+iRM4bPiJpM=
git.blauwelle.com/go/crate/runtimehelper v0.2.0/go.mod h1:yVMA0GkO9AS7iuPmalHKeWyv9en0JWj25rY1vpTuHhk=
Loading…
Cancel
Save