From ebaa33ac2817e36b37f3c789ee30f4cf9cec957e Mon Sep 17 00:00:00 2001 From: Bruce MacDonald Date: Thu, 20 Jul 2023 20:45:12 +0200 Subject: [PATCH 1/2] display gin api errors in cli --- api/client.go | 9 ++++----- api/types.go | 27 +++++++++++++++++---------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/api/client.go b/api/client.go index 8078c428..e2f41348 100644 --- a/api/client.go +++ b/api/client.go @@ -27,7 +27,7 @@ func checkError(resp *http.Response, body []byte) error { err := json.Unmarshal(body, &apiError) if err != nil { // Use the full body as the message if we fail to decode a response. - apiError.Message = string(body) + apiError.ErrorMessage = string(body) } return apiError @@ -92,7 +92,6 @@ func (c *Client) do(ctx context.Context, method, path string, reqData, respData } } return nil - } func (c *Client) stream(ctx context.Context, method, path string, data any, fn func([]byte) error) error { @@ -133,9 +132,9 @@ func (c *Client) stream(ctx context.Context, method, path string, data any, fn f if response.StatusCode >= 400 { return StatusError{ - StatusCode: response.StatusCode, - Status: response.Status, - Message: errorResponse.Error, + StatusCode: response.StatusCode, + Status: response.Status, + ErrorMessage: errorResponse.Error, } } diff --git a/api/types.go b/api/types.go index b14d6811..eee74597 100644 --- a/api/types.go +++ b/api/types.go @@ -8,16 +8,23 @@ import ( ) type StatusError struct { - StatusCode int - Status string - Message string + StatusCode int + Status string + ErrorMessage string `json:"error"` } func (e StatusError) Error() string { - if e.Message != "" { - return fmt.Sprintf("%s: %s", e.Status, e.Message) + switch { + case e.Status != "" && e.ErrorMessage != "": + return fmt.Sprintf("%s: %s", e.Status, e.ErrorMessage) + case e.Status != "": + return e.Status + case e.ErrorMessage != "": + return e.ErrorMessage + default: + // this should not happen + return "something went wrong, please see the ollama server logs for details" } - return e.Status } type GenerateRequest struct { @@ -44,10 +51,10 @@ type PullRequest struct { } type ProgressResponse struct { - Status string `json:"status"` - Digest string `json:"digest,omitempty"` - Total int `json:"total,omitempty"` - Completed int `json:"completed,omitempty"` + Status string `json:"status"` + Digest string `json:"digest,omitempty"` + Total int `json:"total,omitempty"` + Completed int `json:"completed,omitempty"` } type PushRequest struct { From 09dc6273e3404cf6a3dfad3810a081b4226667bb Mon Sep 17 00:00:00 2001 From: Bruce MacDonald Date: Thu, 20 Jul 2023 20:53:09 +0200 Subject: [PATCH 2/2] suppress error when running list before pulling image --- server/routes.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/server/routes.go b/server/routes.go index f97ab120..0e41f9a5 100644 --- a/server/routes.go +++ b/server/routes.go @@ -2,6 +2,7 @@ package server import ( "encoding/json" + "errors" "io" "log" "net" @@ -163,6 +164,10 @@ func list(c *gin.Context) { } err = filepath.Walk(fp, func(path string, info os.FileInfo, err error) error { if err != nil { + if errors.Is(err, os.ErrNotExist) { + log.Printf("manifest file does not exist: %s", fp) + return nil + } return err } if !info.IsDir() {