From 8671f2826d67055b661aa7e23e7e1731ff1b33d1 Mon Sep 17 00:00:00 2001 From: Ge Song Date: Fri, 23 Feb 2024 13:14:16 +0800 Subject: [PATCH] httpdata/add stacktrace; --- httpdata/code.go | 1 + httpdata/data.go | 43 ++++++++++++++++++++++++++++++++++++------- httpdata/error.go | 24 +++++++++++++++--------- httpdata/flag.go | 7 +++++++ httpdata/go.mod | 4 +++- httpdata/go.sum | 2 ++ 6 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 httpdata/flag.go create mode 100644 httpdata/go.sum diff --git a/httpdata/code.go b/httpdata/code.go index b158588..a00deb4 100644 --- a/httpdata/code.go +++ b/httpdata/code.go @@ -11,4 +11,5 @@ const ( CodeUnexpect Code = "unexpect" CodeUnexpectPanic Code = "unexpect.panic" CodeUnknown Code = "unknown" + CodeUnauthorized Code = "unauthorized" ) diff --git a/httpdata/data.go b/httpdata/data.go index 03c1358..bec97e9 100644 --- a/httpdata/data.go +++ b/httpdata/data.go @@ -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, + } +} diff --git a/httpdata/error.go b/httpdata/error.go index 927fd6c..c5bb141 100644 --- a/httpdata/error.go +++ b/httpdata/error.go @@ -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{}) } diff --git a/httpdata/flag.go b/httpdata/flag.go new file mode 100644 index 0000000..5e05737 --- /dev/null +++ b/httpdata/flag.go @@ -0,0 +1,7 @@ +package httpdata + +import ( + "os" +) + +var FlagDebug = os.Getenv("DEBUG") == "true" diff --git a/httpdata/go.mod b/httpdata/go.mod index 5372031..f1c12e7 100644 --- a/httpdata/go.mod +++ b/httpdata/go.mod @@ -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 diff --git a/httpdata/go.sum b/httpdata/go.sum new file mode 100644 index 0000000..a413c81 --- /dev/null +++ b/httpdata/go.sum @@ -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=