Skip to content

Commit 3e852ac

Browse files
author
ddvk
committed
fix for juruen#45
1 parent 5b3d50f commit 3e852ac

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

api/api.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@ type ApiCtx struct {
2121
}
2222

2323
// CreateApiCtx initializes an instance of ApiCtx
24-
func CreateApiCtx(http *transport.HttpClientCtx) *ApiCtx {
25-
return &ApiCtx{http, DocumentsFileTree(http)}
24+
func CreateApiCtx(http *transport.HttpClientCtx) (*ApiCtx, error) {
25+
fileTree, err := DocumentsFileTree(http)
26+
if err != nil {
27+
return nil, fmt.Errorf("failed to fetch document tree %w", err)
28+
}
29+
return &ApiCtx{http, fileTree}, nil
2630
}
2731

2832
// DocumentsFileTree reads your remote documents and builds a file tree
2933
// structure to represent them
30-
func DocumentsFileTree(http *transport.HttpClientCtx) *filetree.FileTreeCtx {
34+
func DocumentsFileTree(http *transport.HttpClientCtx) (*filetree.FileTreeCtx, error) {
3135
documents := make([]model.Document, 0)
3236

3337
if err := http.Get(transport.UserBearer, listDocs, nil, &documents); err != nil {
34-
log.Error.Println("failed to fetch documents", err.Error())
35-
return nil
38+
return nil, err
3639
}
3740

3841
fileTree := filetree.CreateFileTreeCtx()
@@ -45,7 +48,7 @@ func DocumentsFileTree(http *transport.HttpClientCtx) *filetree.FileTreeCtx {
4548
log.Trace.Println(d.Name(), d.IsFile())
4649
}
4750

48-
return &fileTree
51+
return &fileTree, nil
4952
}
5053

5154
// FetchDocument downloads a document given its ID and saves it locally into dstPath

main.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@ func main() {
2323
log.InitLog()
2424

2525
var ctx *api.ApiCtx
26+
var err error
2627
for i := 0; i < AUTH_RETRIES; i++ {
27-
ctx = api.CreateApiCtx(api.AuthHttpCtx())
28+
ctx, err = api.CreateApiCtx(api.AuthHttpCtx())
2829

29-
if ctx.Filetree == nil && i < AUTH_RETRIES {
30-
log.Error.Println("retrying...")
30+
if err != nil {
31+
log.Trace.Println(err)
3132
}
3233
}
3334

34-
if ctx.Filetree == nil {
35-
log.Error.Fatal("failed to build documents tree")
35+
if ctx == nil {
36+
log.Error.Fatal("failed to build documents tree, last error: ", err)
3637
}
3738

3839
run_shell(ctx)

0 commit comments

Comments
 (0)