From 6d6b0d33219322553c2ac70393e347f1bba61367 Mon Sep 17 00:00:00 2001 From: Patrick Devine Date: Fri, 21 Jul 2023 23:02:12 -0700 Subject: [PATCH] change error handler behavior and fix error when a model isn't found (#173) --- api/client.go | 16 +++++----------- cmd/cmd.go | 8 ++------ server/images.go | 10 +++------- server/routes.go | 19 +++++++------------ 4 files changed, 17 insertions(+), 36 deletions(-) diff --git a/api/client.go b/api/client.go index 1330b890..d339da56 100644 --- a/api/client.go +++ b/api/client.go @@ -210,15 +210,9 @@ func (c *Client) List(ctx context.Context) (*ListResponse, error) { return &lr, nil } -type DeleteProgressFunc func(ProgressResponse) error - -func (c *Client) Delete(ctx context.Context, req *DeleteRequest, fn DeleteProgressFunc) error { - return c.stream(ctx, http.MethodDelete, "/api/delete", req, func(bts []byte) error { - var resp ProgressResponse - if err := json.Unmarshal(bts, &resp); err != nil { - return err - } - - return fn(resp) - }) +func (c *Client) Delete(ctx context.Context, req *DeleteRequest) error { + if err := c.do(ctx, http.MethodDelete, "/api/delete", req, nil); err != nil { + return err + } + return nil } diff --git a/cmd/cmd.go b/cmd/cmd.go index ad4e56a9..a40f68b2 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -140,14 +140,10 @@ func DeleteHandler(cmd *cobra.Command, args []string) error { client := api.NewClient() request := api.DeleteRequest{Name: args[0]} - fn := func(resp api.ProgressResponse) error { - fmt.Println(resp.Status) - return nil - } - - if err := client.Delete(context.Background(), &request, fn); err != nil { + if err := client.Delete(context.Background(), &request); err != nil { return err } + fmt.Printf("deleted '%s'\n", args[0]) return nil } diff --git a/server/images.go b/server/images.go index cd732d7b..8b52a3e6 100644 --- a/server/images.go +++ b/server/images.go @@ -108,8 +108,8 @@ func GetManifest(mp ModelPath) (*ManifestV2, error) { return nil, err } - if _, err = os.Stat(fp); err != nil && !errors.Is(err, os.ErrNotExist) { - return nil, fmt.Errorf("couldn't find model '%s'", mp.GetShortTagname()) + if _, err = os.Stat(fp); err != nil { + return nil, err } var manifest *ManifestV2 @@ -493,12 +493,11 @@ func CreateLayer(f io.ReadSeeker) (*LayerReader, error) { return layer, nil } -func DeleteModel(name string, fn func(api.ProgressResponse)) error { +func DeleteModel(name string) error { mp := ParseModelPath(name) manifest, err := GetManifest(mp) if err != nil { - fn(api.ProgressResponse{Status: "couldn't retrieve manifest"}) return err } deleteMap := make(map[string]bool) @@ -509,12 +508,10 @@ func DeleteModel(name string, fn func(api.ProgressResponse)) error { fp, err := GetManifestPath() if err != nil { - fn(api.ProgressResponse{Status: "problem getting manifest path"}) return err } err = filepath.Walk(fp, func(path string, info os.FileInfo, err error) error { if err != nil { - fn(api.ProgressResponse{Status: "problem walking manifest dir"}) return err } if !info.IsDir() { @@ -569,7 +566,6 @@ func DeleteModel(name string, fn func(api.ProgressResponse)) error { log.Printf("couldn't remove manifest file '%s': %v", fp, err) return err } - fn(api.ProgressResponse{Status: fmt.Sprintf("deleted '%s'", name)}) return nil } diff --git a/server/routes.go b/server/routes.go index 0d7edaf0..1bd361b5 100644 --- a/server/routes.go +++ b/server/routes.go @@ -3,6 +3,7 @@ package server import ( "encoding/json" "errors" + "fmt" "io" "log" "net" @@ -166,20 +167,14 @@ func DeleteModelHandler(c *gin.Context) { return } - ch := make(chan any) - go func() { - defer close(ch) - fn := func(r api.ProgressResponse) { - ch <- r - } - - if err := DeleteModel(req.Name, fn); err != nil { + if err := DeleteModel(req.Name); err != nil { + if os.IsNotExist(err) { + c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("model '%s' not found", req.Name)}) + } else { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - return } - }() - - streamResponse(c, ch) + return + } } func ListModelsHandler(c *gin.Context) {