浏览代码

return hint in error function

Bruce MacDonald 2 月之前
父节点
当前提交
a5f2db3744
共有 3 个文件被更改,包括 8 次插入24 次删除
  1. 4 1
      api/types.go
  2. 3 22
      main.go
  3. 1 1
      server/routes.go

+ 4 - 1
api/types.go

@@ -647,7 +647,10 @@ type ErrorResponse struct {
 }
 }
 
 
 func (e ErrorResponse) Error() string {
 func (e ErrorResponse) Error() string {
-	return e.Err
+	if e.Hint == "" {
+		return e.Err
+	}
+	return fmt.Sprintf("%s\n%s", e.Err, e.Hint)
 }
 }
 
 
 // FormatParams converts specified parameter options to their correct types
 // FormatParams converts specified parameter options to their correct types

+ 3 - 22
main.go

@@ -2,31 +2,12 @@ package main
 
 
 import (
 import (
 	"context"
 	"context"
-	"fmt"
-	"os"
 
 
-	"github.com/ollama/ollama/api"
+	"github.com/spf13/cobra"
+
 	"github.com/ollama/ollama/cmd"
 	"github.com/ollama/ollama/cmd"
 )
 )
 
 
 func main() {
 func main() {
-	checkErr(cmd.NewCLI().ExecuteContext(context.Background()))
-}
-
-// checkErr prints the error message and exits the program if the message is not nil.
-func checkErr(err any) {
-	if err == nil {
-		return
-	}
-
-	switch e := err.(type) {
-	case api.ErrorResponse:
-		fmt.Fprintln(os.Stderr, "Error: ", e.Err)
-		if e.Hint != "" {
-			fmt.Fprintf(os.Stderr, "\n%s\n", e.Hint)
-		}
-	default:
-		fmt.Fprintln(os.Stderr, "Error: ", err)
-	}
-	os.Exit(1)
+	cobra.CheckErr(cmd.NewCLI().ExecuteContext(context.Background()))
 }
 }

+ 1 - 1
server/routes.go

@@ -596,7 +596,7 @@ func (s *Server) PullHandler(c *gin.Context) {
 			if errors.As(err, &e) {
 			if errors.As(err, &e) {
 				hint := fmt.Sprintf("Model %q not found - please check the model name is correct and try again", reqName)
 				hint := fmt.Sprintf("Model %q not found - please check the model name is correct and try again", reqName)
 				if name.Host == DefaultRegistry {
 				if name.Host == DefaultRegistry {
-					hint = fmt.Sprintf("Model %q not found - search available models at https://ollama.com/search?q=%s", reqName, reqName)
+					hint = fmt.Sprintf("Model %q not found - search available models at: https://ollama.com/search?q=%s", reqName, reqName)
 				}
 				}
 				ch <- api.ErrorResponse{
 				ch <- api.ErrorResponse{
 					Err:  err.Error(),
 					Err:  err.Error(),