Skip to content

Commit accfe59

Browse files
committed
implement a custom type for ErrNotFound
Signed-off-by: Grant Linville <[email protected]>
1 parent 8d1d053 commit accfe59

File tree

3 files changed

+39
-32
lines changed

3 files changed

+39
-32
lines changed

gptscript.go

+19-30
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"fmt"
1111
"io"
1212
"log/slog"
13-
"net/http"
1413
"os"
1514
"os/exec"
1615
"path/filepath"
@@ -162,7 +161,7 @@ func (g *GPTScript) Parse(ctx context.Context, fileName string, opts ...ParseOpt
162161
disableCache = disableCache || opt.DisableCache
163162
}
164163

165-
out, _, err := g.runBasicCommand(ctx, "parse", map[string]any{"file": fileName, "disableCache": disableCache})
164+
out, err := g.runBasicCommand(ctx, "parse", map[string]any{"file": fileName, "disableCache": disableCache})
166165
if err != nil {
167166
return nil, err
168167
}
@@ -181,7 +180,7 @@ func (g *GPTScript) Parse(ctx context.Context, fileName string, opts ...ParseOpt
181180

182181
// ParseContent will parse the given string into a tool.
183182
func (g *GPTScript) ParseContent(ctx context.Context, toolDef string) ([]Node, error) {
184-
out, _, err := g.runBasicCommand(ctx, "parse", map[string]any{"content": toolDef})
183+
out, err := g.runBasicCommand(ctx, "parse", map[string]any{"content": toolDef})
185184
if err != nil {
186185
return nil, err
187186
}
@@ -204,7 +203,7 @@ func (g *GPTScript) Fmt(ctx context.Context, nodes []Node) (string, error) {
204203
node.TextNode.combine()
205204
}
206205

207-
out, _, err := g.runBasicCommand(ctx, "fmt", Document{Nodes: nodes})
206+
out, err := g.runBasicCommand(ctx, "fmt", Document{Nodes: nodes})
208207
if err != nil {
209208
return "", err
210209
}
@@ -242,7 +241,7 @@ func (g *GPTScript) load(ctx context.Context, payload map[string]any, opts ...Lo
242241
}
243242
}
244243

245-
out, _, err := g.runBasicCommand(ctx, "load", payload)
244+
out, err := g.runBasicCommand(ctx, "load", payload)
246245
if err != nil {
247246
return nil, err
248247
}
@@ -261,7 +260,7 @@ func (g *GPTScript) load(ctx context.Context, payload map[string]any, opts ...Lo
261260

262261
// Version will return the output of `gptscript --version`
263262
func (g *GPTScript) Version(ctx context.Context) (string, error) {
264-
out, _, err := g.runBasicCommand(ctx, "version", nil)
263+
out, err := g.runBasicCommand(ctx, "version", nil)
265264
if err != nil {
266265
return "", err
267266
}
@@ -286,7 +285,7 @@ func (g *GPTScript) ListModels(ctx context.Context, opts ...ListModelsOptions) (
286285
o.Providers = append(o.Providers, g.globalOpts.DefaultModelProvider)
287286
}
288287

289-
out, _, err := g.runBasicCommand(ctx, "list-models", map[string]any{
288+
out, err := g.runBasicCommand(ctx, "list-models", map[string]any{
290289
"providers": o.Providers,
291290
"env": g.globalOpts.Env,
292291
"credentialOverrides": o.CredentialOverrides,
@@ -299,12 +298,12 @@ func (g *GPTScript) ListModels(ctx context.Context, opts ...ListModelsOptions) (
299298
}
300299

301300
func (g *GPTScript) Confirm(ctx context.Context, resp AuthResponse) error {
302-
_, _, err := g.runBasicCommand(ctx, "confirm/"+resp.ID, resp)
301+
_, err := g.runBasicCommand(ctx, "confirm/"+resp.ID, resp)
303302
return err
304303
}
305304

306305
func (g *GPTScript) PromptResponse(ctx context.Context, resp PromptResponse) error {
307-
_, _, err := g.runBasicCommand(ctx, "prompt-response/"+resp.ID, resp.Responses)
306+
_, err := g.runBasicCommand(ctx, "prompt-response/"+resp.ID, resp.Responses)
308307
return err
309308
}
310309

@@ -323,7 +322,7 @@ func (g *GPTScript) ListCredentials(ctx context.Context, opts ListCredentialsOpt
323322
req.Context = []string{"default"}
324323
}
325324

326-
out, _, err := g.runBasicCommand(ctx, "credentials", req)
325+
out, err := g.runBasicCommand(ctx, "credentials", req)
327326
if err != nil {
328327
return nil, err
329328
}
@@ -341,12 +340,12 @@ func (g *GPTScript) CreateCredential(ctx context.Context, cred Credential) error
341340
return fmt.Errorf("failed to marshal credential: %w", err)
342341
}
343342

344-
_, _, err = g.runBasicCommand(ctx, "credentials/create", CredentialRequest{Content: string(credJSON)})
343+
_, err = g.runBasicCommand(ctx, "credentials/create", CredentialRequest{Content: string(credJSON)})
345344
return err
346345
}
347346

348347
func (g *GPTScript) RevealCredential(ctx context.Context, credCtxs []string, name string) (Credential, error) {
349-
out, _, err := g.runBasicCommand(ctx, "credentials/reveal", CredentialRequest{
348+
out, err := g.runBasicCommand(ctx, "credentials/reveal", CredentialRequest{
350349
Context: credCtxs,
351350
Name: name,
352351
})
@@ -361,25 +360,15 @@ func (g *GPTScript) RevealCredential(ctx context.Context, credCtxs []string, nam
361360
return cred, nil
362361
}
363362

364-
// DeleteCredential will delete the credential with the given name in the given context.
365-
// A return value of false, nil indicates that the credential was not found.
366-
// false, non-nil error indicates a different error when trying to delete.
367-
// true, nil indicates a successful deletion.
368-
func (g *GPTScript) DeleteCredential(ctx context.Context, credCtx, name string) (bool, error) {
369-
_, code, err := g.runBasicCommand(ctx, "credentials/delete", CredentialRequest{
363+
func (g *GPTScript) DeleteCredential(ctx context.Context, credCtx, name string) error {
364+
_, err := g.runBasicCommand(ctx, "credentials/delete", CredentialRequest{
370365
Context: []string{credCtx}, // Only one context can be specified for delete operations
371366
Name: name,
372367
})
373-
if err != nil {
374-
if code == http.StatusNotFound {
375-
return false, nil
376-
}
377-
return false, err
378-
}
379-
return true, nil
368+
return err
380369
}
381370

382-
func (g *GPTScript) runBasicCommand(ctx context.Context, requestPath string, body any) (string, int, error) {
371+
func (g *GPTScript) runBasicCommand(ctx context.Context, requestPath string, body any) (string, error) {
383372
run := &Run{
384373
url: g.url,
385374
requestPath: requestPath,
@@ -388,18 +377,18 @@ func (g *GPTScript) runBasicCommand(ctx context.Context, requestPath string, bod
388377
}
389378

390379
if err := run.request(ctx, body); err != nil {
391-
return "", run.responseCode, err
380+
return "", err
392381
}
393382

394383
out, err := run.Text()
395384
if err != nil {
396-
return "", run.responseCode, err
385+
return "", err
397386
}
398387
if run.err != nil {
399-
return run.ErrorOutput(), run.responseCode, run.err
388+
return run.ErrorOutput(), run.err
400389
}
401390

402-
return out, run.responseCode, nil
391+
return out, nil
403392
}
404393

405394
func getCommand() string {

gptscript_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gptscript
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"math/rand"
78
"os"
@@ -1484,7 +1485,11 @@ func TestCredentials(t *testing.T) {
14841485
require.Equal(t, cred.RefreshToken, "my-refresh-token")
14851486

14861487
// Delete
1487-
found, err := g.DeleteCredential(context.Background(), "testing", name)
1488+
err = g.DeleteCredential(context.Background(), "testing", name)
14881489
require.NoError(t, err)
1489-
require.True(t, found)
1490+
1491+
// Delete again and make sure we get a NotFoundError
1492+
err = g.DeleteCredential(context.Background(), "testing", name)
1493+
require.Error(t, err)
1494+
require.True(t, errors.As(err, &ErrNotFound{}))
14901495
}

run.go

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ import (
1717

1818
var errAbortRun = errors.New("run aborted")
1919

20+
type ErrNotFound struct {
21+
Message string
22+
}
23+
24+
func (e ErrNotFound) Error() string {
25+
return e.Message
26+
}
27+
2028
type Run struct {
2129
url, requestPath, toolPath string
2230
tools []ToolDef
@@ -61,6 +69,11 @@ func (r *Run) State() RunState {
6169
// Err returns the error that caused the gptscript to fail, if any.
6270
func (r *Run) Err() error {
6371
if r.err != nil {
72+
if r.responseCode == http.StatusNotFound {
73+
return ErrNotFound{
74+
Message: fmt.Sprintf("run encountered an error: %s", r.errput),
75+
}
76+
}
6477
return fmt.Errorf("run encountered an error: %w with error output: %s", r.err, r.errput)
6578
}
6679
return nil

0 commit comments

Comments
 (0)