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
}
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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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) {