Compare commits

..

2 Commits

Author SHA1 Message Date
8671f2826d httpdata/add stacktrace; 2024-02-23 13:14:16 +08:00
5cf4430bc8 uptracehelper/change GoStop to Bootstrap; 2024-01-17 17:24:32 +08:00
7 changed files with 77 additions and 26 deletions

View File

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

View File

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

View File

@@ -2,11 +2,24 @@ package httpdata
import ( import (
"errors" "errors"
"git.blauwelle.com/go/crate/runtimehelper"
) )
const maximumFrames = 8
type UniverseError struct { type UniverseError struct {
Code Code Code Code
Message string 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 { 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 { func IsUniverseError(err error) bool {
return errors.As(err, &UniverseError{}) return errors.As(err, &UniverseError{})
} }

7
httpdata/flag.go Normal file
View File

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

View File

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

2
httpdata/go.sum Normal file
View File

@@ -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=

View File

@@ -2,6 +2,7 @@ package uptracehelper
import ( import (
"context" "context"
"time"
"github.com/uptrace/uptrace-go/uptrace" "github.com/uptrace/uptrace-go/uptrace"
) )
@@ -10,6 +11,7 @@ type Config struct {
ServiceName string ServiceName string
ServiceVersion string ServiceVersion string
DeploymentEnvironment string DeploymentEnvironment string
ShutdownTimeout time.Duration
} }
func Setup(cfg Config) { func Setup(cfg Config) {
@@ -23,19 +25,21 @@ func Setup(cfg Config) {
uptrace.ConfigureOpentelemetry(opts...) uptrace.ConfigureOpentelemetry(opts...)
} }
// GoStop 初始化并等待 uptrace // Bootstrap 初始化并等待 uptrace
// 配合 [git.blauwelle.com/go/crate/exegroup] 使用 // 配合 [git.blauwelle.com/go/crate/exegroup] 使用
// env // env
// - UPTRACE_DISABLED 存在就不再初始化 // - UPTRACE_DISABLED 存在就不再初始化
// - UPTRACE_DSN 服务端地址 // - UPTRACE_DSN 服务端地址
func GoStop(cfg Config) (func(ctx context.Context) error, func(ctx context.Context)) { func Bootstrap(cfg Config) func(ctx context.Context) error {
Setup(cfg) Setup(cfg)
shutdownErr := make(chan error, 1) return func(ctx context.Context) error {
goFunc := func(context.Context) error { <-ctx.Done()
return <-shutdownErr shutdownCtx := context.Background()
if cfg.ShutdownTimeout >= 0 {
var cancel func()
shutdownCtx, cancel = context.WithTimeout(shutdownCtx, cfg.ShutdownTimeout)
defer cancel()
} }
stopFunc := func(ctx context.Context) { return uptrace.Shutdown(shutdownCtx)
shutdownErr <- uptrace.Shutdown(ctx)
} }
return goFunc, stopFunc
} }