Browse Source

add not found handle

fancl 2 years ago
parent
commit
3a96d8425b
2 changed files with 8 additions and 2 deletions
  1. 4 2
      gateway/cli/executor.go
  2. 4 0
      gateway/cli/server.go

+ 4 - 2
gateway/cli/executor.go

@@ -28,7 +28,7 @@ type Executor struct {
 	seq            int64
 	locker         sync.RWMutex
 	params         []string //params (eg: /show/user/:uid => :uid)
-	NotFoundHandle func(ctx *Context, cmd string) (*Response, error)
+	NotFoundHandle HandleFunc
 }
 
 type (
@@ -184,7 +184,9 @@ func (exec *Executor) Do(ctx *Context, args ...string) (res *Response, err error
 	}
 	if exec.handleFunc == nil {
 		if exec.NotFoundHandle != nil {
-			res, err = exec.NotFoundHandle(ctx, ctx.CmdStr)
+			if err = exec.NotFoundHandle(ctx); err == nil {
+				res = ctx.response
+			}
 		} else {
 			err = fmt.Errorf("%s not found", ctx.CmdStr)
 		}

+ 4 - 0
gateway/cli/server.go

@@ -154,6 +154,10 @@ func (svr *Server) formatDescription(ss ...string) (usage string, description st
 	return
 }
 
+func (svr *Server) NotFoundHandle(cb HandleFunc) {
+	svr.executor.NotFoundHandle = cb
+}
+
 func (svr *Server) Handle(path string, cb HandleFunc, cbs ...Option) {
 	var (
 		err    error