refactor: improve error handling and fix linter warnings

- Add nolint comments for type assertion errcheck in gjson/encode.go
  (switch case guarantees type safety)
- Handle fasthttp.Serve errors in benchmark mock backends
- Rename error variables to avoid shadowing in server.go
- Use underscore for unused loop variables

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-05-09 17:32:06 +08:00
parent c157be1ce5
commit 8643a0aff9
3 changed files with 34 additions and 18 deletions

View File

@ -23,11 +23,11 @@ func encodeLuaValue(L *glua.LState, value glua.LValue, config *Config, depth int
return "false", nil
case glua.LTNumber:
num := float64(value.(glua.LNumber))
num := float64(value.(glua.LNumber)) //nolint:errcheck // switch case guarantees type
return formatNumber(num, config.encodeNumberPrecision), nil
case glua.LTString:
str := string(value.(glua.LString))
str := string(value.(glua.LString)) //nolint:errcheck // switch case guarantees type
// Use go-json for proper string escaping
result, err := json.Marshal(str)
if err != nil {
@ -36,7 +36,7 @@ func encodeLuaValue(L *glua.LState, value glua.LValue, config *Config, depth int
return string(result), nil
case glua.LTTable:
return encodeTable(L, value.(*glua.LTable), config, depth)
return encodeTable(L, value.(*glua.LTable), config, depth) //nolint:errcheck // switch case guarantees type
case glua.LTUserData:
if isNull(value) {
@ -82,7 +82,7 @@ func checkArrayType(tbl *glua.LTable, config *Config) (bool, int, int) {
count := 0
hasStringKey := false
tbl.ForEach(func(key, value glua.LValue) {
tbl.ForEach(func(key, _ glua.LValue) {
switch k := key.(type) {
case glua.LNumber:
// Protect against integer overflow - only accept positive integers within int range

View File

@ -61,10 +61,14 @@ func SimpleMockBackend(statusCode int, responseBody []byte) (string, func()) {
ctx.SetBody(responseBody)
}
go fasthttp.Serve(ln, handler)
go func() {
if serveErr := fasthttp.Serve(ln, handler); serveErr != nil {
panic(serveErr)
}
}()
return ln.Addr().String(), func() {
ln.Close()
_ = ln.Close()
}
}
@ -87,10 +91,14 @@ func ErrorMockBackend(errorRate float64, errorBody []byte) (string, func()) {
ctx.SetBody([]byte("OK"))
}
go fasthttp.Serve(ln, handler)
go func() {
if serveErr := fasthttp.Serve(ln, handler); serveErr != nil {
panic(serveErr)
}
}()
return ln.Addr().String(), func() {
ln.Close()
_ = ln.Close()
}
}
@ -107,10 +115,14 @@ func DelayedMockBackend(delay time.Duration, statusCode int, responseBody []byte
ctx.SetBody(responseBody)
}
go fasthttp.Serve(ln, handler)
go func() {
if serveErr := fasthttp.Serve(ln, handler); serveErr != nil {
panic(serveErr)
}
}()
return ln.Addr().String(), func() {
ln.Close()
_ = ln.Close()
}
}
@ -154,9 +166,13 @@ func StartMockFasthttpBackend(config MockBackendConfig) (string, func()) {
}
}
go fasthttp.Serve(ln, handler)
go func() {
if serveErr := fasthttp.Serve(ln, handler); serveErr != nil {
panic(serveErr)
}
}()
return ln.Addr().String(), func() {
ln.Close()
_ = ln.Close()
}
}

View File

@ -649,9 +649,9 @@ func (s *Server) startMultiServerMode() error {
// 注册状态监控端点(仅默认服务器)
if serverCfg.Default && s.config.Monitoring.Status.Enabled {
statusHandler, err := NewStatusHandler(s, &s.config.Monitoring.Status)
if err != nil {
logging.Error().Msg("Failed to create status handler: " + err.Error())
statusHandler, statusErr := NewStatusHandler(s, &s.config.Monitoring.Status)
if statusErr != nil {
logging.Error().Msg("Failed to create status handler: " + statusErr.Error())
} else {
router.GET(statusHandler.Path(), statusHandler.ServeHTTP)
}
@ -659,9 +659,9 @@ func (s *Server) startMultiServerMode() error {
// 注册 pprof 性能分析端点(仅默认服务器)
if serverCfg.Default && s.config.Monitoring.Pprof.Enabled {
pprofHandler, err := NewPprofHandler(&s.config.Monitoring.Pprof)
if err != nil {
logging.Error().Msg("Failed to create pprof handler: " + err.Error())
pprofHandler, pprofErr := NewPprofHandler(&s.config.Monitoring.Pprof)
if pprofErr != nil {
logging.Error().Msg("Failed to create pprof handler: " + pprofErr.Error())
} else {
router.GET(pprofHandler.Path(), pprofHandler.ServeHTTP)
router.GET(pprofHandler.Path()+"/{profile:*}", pprofHandler.ServeHTTP)