change error handler behavior and fix error when a model isn't found (#173)

This commit is contained in:
Patrick Devine 2023-07-21 23:02:12 -07:00 committed by GitHub
parent 37324a0a00
commit 6d6b0d3321
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 36 deletions

View file

@ -210,15 +210,9 @@ func (c *Client) List(ctx context.Context) (*ListResponse, error) {
return &lr, nil return &lr, nil
} }
type DeleteProgressFunc func(ProgressResponse) error func (c *Client) Delete(ctx context.Context, req *DeleteRequest) error {
if err := c.do(ctx, http.MethodDelete, "/api/delete", req, nil); err != nil {
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 err
} }
return nil
return fn(resp)
})
} }

View file

@ -140,14 +140,10 @@ func DeleteHandler(cmd *cobra.Command, args []string) error {
client := api.NewClient() client := api.NewClient()
request := api.DeleteRequest{Name: args[0]} request := api.DeleteRequest{Name: args[0]}
fn := func(resp api.ProgressResponse) error { if err := client.Delete(context.Background(), &request); err != nil {
fmt.Println(resp.Status)
return nil
}
if err := client.Delete(context.Background(), &request, fn); err != nil {
return err return err
} }
fmt.Printf("deleted '%s'\n", args[0])
return nil return nil
} }

View file

@ -108,8 +108,8 @@ func GetManifest(mp ModelPath) (*ManifestV2, error) {
return nil, err return nil, err
} }
if _, err = os.Stat(fp); err != nil && !errors.Is(err, os.ErrNotExist) { if _, err = os.Stat(fp); err != nil {
return nil, fmt.Errorf("couldn't find model '%s'", mp.GetShortTagname()) return nil, err
} }
var manifest *ManifestV2 var manifest *ManifestV2
@ -493,12 +493,11 @@ func CreateLayer(f io.ReadSeeker) (*LayerReader, error) {
return layer, nil return layer, nil
} }
func DeleteModel(name string, fn func(api.ProgressResponse)) error { func DeleteModel(name string) error {
mp := ParseModelPath(name) mp := ParseModelPath(name)
manifest, err := GetManifest(mp) manifest, err := GetManifest(mp)
if err != nil { if err != nil {
fn(api.ProgressResponse{Status: "couldn't retrieve manifest"})
return err return err
} }
deleteMap := make(map[string]bool) deleteMap := make(map[string]bool)
@ -509,12 +508,10 @@ func DeleteModel(name string, fn func(api.ProgressResponse)) error {
fp, err := GetManifestPath() fp, err := GetManifestPath()
if err != nil { if err != nil {
fn(api.ProgressResponse{Status: "problem getting manifest path"})
return err return err
} }
err = filepath.Walk(fp, func(path string, info os.FileInfo, err error) error { err = filepath.Walk(fp, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
fn(api.ProgressResponse{Status: "problem walking manifest dir"})
return err return err
} }
if !info.IsDir() { 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) log.Printf("couldn't remove manifest file '%s': %v", fp, err)
return err return err
} }
fn(api.ProgressResponse{Status: fmt.Sprintf("deleted '%s'", name)})
return nil return nil
} }

View file

@ -3,6 +3,7 @@ package server
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io" "io"
"log" "log"
"net" "net"
@ -166,20 +167,14 @@ func DeleteModelHandler(c *gin.Context) {
return return
} }
ch := make(chan any) if err := DeleteModel(req.Name); err != nil {
go func() { if os.IsNotExist(err) {
defer close(ch) c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("model '%s' not found", req.Name)})
fn := func(r api.ProgressResponse) { } else {
ch <- r
}
if err := DeleteModel(req.Name, fn); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
return return
} }
}()
streamResponse(c, ch)
} }
func ListModelsHandler(c *gin.Context) { func ListModelsHandler(c *gin.Context) {