-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
59 lines (51 loc) · 1.47 KB
/
main.go
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
package main
import (
"context"
"fmt"
"os"
"github.com/coze-dev/coze-go"
)
// This examples demonstrates how to handle different types of errors from the Coze API.
func main() {
// Get an access_token through personal access token or oauth.
token := os.Getenv("COZE_API_TOKEN")
authCli := coze.NewTokenAuth(token)
// Init the Coze client through the access_token.
cozeCli := coze.NewCozeAPI(authCli, coze.WithBaseURL(os.Getenv("COZE_API_BASE")))
ctx := context.Background()
// Example 1: Handle API error
_, err := cozeCli.Bots.Retrieve(ctx, &coze.RetrieveBotsReq{
BotID: "invalid_bot_id",
})
if err != nil {
if cozeErr, ok := coze.AsCozeError(err); ok {
// Handle Coze API error
fmt.Printf("Coze API error: %s (code: %s)\n", cozeErr.Message, cozeErr.Code)
return
}
// Handle other errors
fmt.Printf("Other error: %v\n", err)
return
}
// Example 2: Handle auth error
invalidToken := "invalid_token"
invalidAuthCli := coze.NewTokenAuth(invalidToken)
invalidCozeCli := coze.NewCozeAPI(invalidAuthCli)
_, err = invalidCozeCli.Bots.List(ctx, &coze.ListBotsReq{
PageNum: 1,
PageSize: 10,
})
if err != nil {
if cozeErr, ok := coze.AsAuthError(err); ok {
// Handle auth error
if cozeErr.Code == "unauthorized" {
fmt.Println("Authentication failed. Please check your token.")
return
}
fmt.Printf("Coze API error: %s (code: %s)\n", cozeErr.ErrorMessage, cozeErr.Code)
return
}
fmt.Printf("Other error: %v\n", err)
return
}
}