-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
71 lines (61 loc) · 2.21 KB
/
Copy patherrors_test.go
File metadata and controls
71 lines (61 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package promptjuggler_test
import (
"context"
"errors"
"net/http"
"testing"
promptjuggler "go.promptjuggler.com/sdk"
)
func TestNon2xxBecomesAPIError(t *testing.T) {
m := newMockServer(http.StatusNotFound, `{"error":"Prompt run not found"}`)
defer m.close()
_, err := m.client().GetPromptRun(context.Background(), uuid1)
var apiErr *promptjuggler.APIError
if !errors.As(err, &apiErr) {
t.Fatalf("error = %T (%v), want *APIError", err, err)
}
if apiErr.StatusCode != http.StatusNotFound {
t.Errorf("StatusCode = %d", apiErr.StatusCode)
}
if apiErr.Message != "Prompt run not found" {
t.Errorf("Message = %q", apiErr.Message)
}
}
func TestServerErrorBecomesAPIError(t *testing.T) {
m := newMockServer(http.StatusInternalServerError, `{"error":"boom"}`)
defer m.close()
_, err := m.client().GetPromptRun(context.Background(), uuid1)
var apiErr *promptjuggler.APIError
if !errors.As(err, &apiErr) || apiErr.StatusCode != http.StatusInternalServerError {
t.Fatalf("error = %v, want *APIError(500)", err)
}
}
func TestConnectionFailureBecomesNetworkError(t *testing.T) {
// Nothing is listening on port 1 — the request never gets a response.
c := promptjuggler.New("test-key", promptjuggler.WithBaseURL("http://127.0.0.1:1"))
_, err := c.GetPromptRun(context.Background(), uuid1)
var netErr *promptjuggler.NetworkError
if !errors.As(err, &netErr) {
t.Fatalf("error = %T (%v), want *NetworkError", err, err)
}
if netErr.Unwrap() == nil {
t.Errorf("NetworkError should wrap the underlying cause")
}
}
func TestUndecodable2xxBecomesDecodeError(t *testing.T) {
// A 200 whose body is missing required PromptRevision fields: the generated client decodes
// it as an error, which must surface as a DecodeError — not a bogus APIError{200}.
m := newMockServer(http.StatusOK, `{}`)
defer m.close()
_, err := m.client().GetPrompt(context.Background(), "greeting", "production")
var decErr *promptjuggler.DecodeError
if !errors.As(err, &decErr) {
t.Fatalf("error = %T (%v), want *DecodeError", err, err)
}
if decErr.StatusCode != http.StatusOK {
t.Errorf("StatusCode = %d, want 200", decErr.StatusCode)
}
if decErr.Unwrap() == nil {
t.Errorf("DecodeError should wrap the underlying decode failure")
}
}