print error stack in faltal error for ghttp.Server

This commit is contained in:
John Guo 2021-10-06 12:12:59 +08:00
parent c84e79a46d
commit 9f2eeb23ba
5 changed files with 26 additions and 22 deletions

View File

@ -209,7 +209,7 @@ func (s *Server) Start() error {
// Install external plugins.
for _, p := range s.plugins {
if err := p.Install(s); err != nil {
s.Logger().Fatal(ctx, err)
s.Logger().Fatalf(ctx, `%+v`, err)
}
}
// Check the group routes again.
@ -383,7 +383,7 @@ func (s *Server) Run() {
ctx = context.TODO()
)
if err := s.Start(); err != nil {
s.Logger().Fatal(ctx, err)
s.Logger().Fatalf(ctx, `%+v`, err)
}
// Blocking using channel.
<-s.closeChan
@ -512,7 +512,7 @@ func (s *Server) startServer(fdMap listenerFdMap) {
}
// The process exits if the server is closed with none closing error.
if err != nil && !strings.EqualFold(http.ErrServerClosed.Error(), err.Error()) {
s.Logger().Fatal(ctx, err)
s.Logger().Fatalf(ctx, `%+v`, err)
}
// If all the underlying servers shutdown, the process exits.
if s.serverCount.Add(-1) < 1 {

View File

@ -56,7 +56,7 @@ func (s *Server) SetServerRoot(root string) {
)
if !gres.Contains(realPath) {
if p, err := gfile.Search(root); err != nil {
s.Logger().Fatal(ctx, `SetServerRoot failed: %v`, err)
s.Logger().Fatalf(ctx, `SetServerRoot failed: %+v`, err)
} else {
realPath = p
}
@ -74,7 +74,7 @@ func (s *Server) AddSearchPath(path string) {
)
if !gres.Contains(realPath) {
if p, err := gfile.Search(path); err != nil {
s.Logger().Fatalf(ctx, `AddSearchPath failed: %v`, err)
s.Logger().Fatalf(ctx, `AddSearchPath failed: %+v`, err)
} else {
realPath = p
}
@ -91,7 +91,7 @@ func (s *Server) AddStaticPath(prefix string, path string) {
)
if !gres.Contains(realPath) {
if p, err := gfile.Search(path); err != nil {
s.Logger().Fatalf(ctx, `AddStaticPath failed: %v`, err)
s.Logger().Fatalf(ctx, `AddStaticPath failed: %+v`, err)
} else {
realPath = p
}

View File

@ -75,11 +75,11 @@ func (s *Server) setHandler(ctx context.Context, pattern string, handler *handle
}
domain, method, uri, err := s.parsePattern(pattern)
if err != nil {
s.Logger().Fatal(ctx, "invalid pattern:", pattern, err)
s.Logger().Fatalf(ctx, `invalid pattern "%s", %+v`, pattern, err)
return
}
if len(uri) == 0 || uri[0] != '/' {
s.Logger().Fatal(ctx, "invalid pattern:", pattern, "URI should lead with '/'")
s.Logger().Fatalf(ctx, `invalid pattern "%s", URI should lead with '/'`, pattern)
return
}

View File

@ -28,7 +28,7 @@ func (s *Server) BindHandler(pattern string, handler interface{}) {
)
funcInfo, err := s.checkAndCreateFuncInfo(handler, "", "", "")
if err != nil {
s.Logger().Fatal(ctx, err)
s.Logger().Fatalf(ctx, `%+v`, err)
}
s.doBindHandler(ctx, pattern, funcInfo, nil, "")
}
@ -131,13 +131,13 @@ func (s *Server) checkAndCreateFuncInfo(f interface{}, pkgPath, structName, meth
if pkgPath != "" {
err = gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid handler: %s.%s.%s defined as "%s", but "func(*ghttp.Request)" or "func(context.Context, Request)(Response, error)" is required`,
`invalid handler: %s.%s.%s defined as "%s", but "func(*ghttp.Request)" or "func(context.Context, BizRequest)(BizResponse, error)" is required`,
pkgPath, structName, methodName, reflect.TypeOf(f).String(),
)
} else {
err = gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid handler: defined as "%s", but "func(*ghttp.Request)" or "func(context.Context, Request)(Response, error)" is required`,
`invalid handler: defined as "%s", but "func(*ghttp.Request)" or "func(context.Context, BizRequest)(BizResponse, error)" is required`,
reflect.TypeOf(f).String(),
)
}

View File

@ -64,7 +64,7 @@ func (s *Server) doBindObject(ctx context.Context, pattern string, object interf
// it removes for convenience for next statement control.
domain, method, path, err := s.parsePattern(pattern)
if err != nil {
s.Logger().Fatal(ctx, err)
s.Logger().Fatalf(ctx, `%+v`, err)
return
}
if strings.EqualFold(method, defaultMethod) {
@ -109,7 +109,7 @@ func (s *Server) doBindObject(ctx context.Context, pattern string, object interf
funcInfo, err := s.checkAndCreateFuncInfo(v.Method(i).Interface(), pkgPath, objName, methodName)
if err != nil {
s.Logger().Fatal(ctx, err)
s.Logger().Fatalf(ctx, `%+v`, err)
}
key := s.mergeBuildInNameToPattern(pattern, structName, methodName, true)
@ -165,11 +165,13 @@ func (s *Server) doBindObjectMethod(ctx context.Context, pattern string, object
v = newValue
t = v.Type()
}
structName := t.Elem().Name()
methodName := strings.TrimSpace(method)
methodValue := v.MethodByName(methodName)
var (
structName = t.Elem().Name()
methodName = strings.TrimSpace(method)
methodValue = v.MethodByName(methodName)
)
if !methodValue.IsValid() {
s.Logger().Fatal(ctx, "invalid method name: "+methodName)
s.Logger().Fatalf(ctx, "invalid method name: %s", methodName)
return
}
if v.MethodByName("Init").IsValid() {
@ -178,16 +180,18 @@ func (s *Server) doBindObjectMethod(ctx context.Context, pattern string, object
if v.MethodByName("Shut").IsValid() {
shutFunc = v.MethodByName("Shut").Interface().(func(*Request))
}
pkgPath := t.Elem().PkgPath()
pkgName := gfile.Basename(pkgPath)
objName := gstr.Replace(t.String(), fmt.Sprintf(`%s.`, pkgName), "")
var (
pkgPath = t.Elem().PkgPath()
pkgName = gfile.Basename(pkgPath)
objName = gstr.Replace(t.String(), fmt.Sprintf(`%s.`, pkgName), "")
)
if objName[0] == '*' {
objName = fmt.Sprintf(`(%s)`, objName)
}
funcInfo, err := s.checkAndCreateFuncInfo(methodValue.Interface(), pkgPath, objName, methodName)
if err != nil {
s.Logger().Fatal(ctx, err)
s.Logger().Fatalf(ctx, `%+v`, err)
}
key := s.mergeBuildInNameToPattern(pattern, structName, methodName, false)
@ -241,7 +245,7 @@ func (s *Server) doBindObjectRest(ctx context.Context, pattern string, object in
funcInfo, err := s.checkAndCreateFuncInfo(v.Method(i).Interface(), pkgPath, objName, methodName)
if err != nil {
s.Logger().Fatal(ctx, err)
s.Logger().Fatalf(ctx, `%+v`, err)
}
key := s.mergeBuildInNameToPattern(methodName+":"+pattern, structName, methodName, false)