/
client_error.go
40 lines (33 loc) · 971 Bytes
/
client_error.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
package lib
import (
"context"
"fmt"
"io"
"os"
files_sdk "github.com/Files-com/files-sdk-go/v2"
)
func ClientError(ctx context.Context, profile *Profiles, err error, out ...io.Writer) {
if len(out) == 0 {
out = append(out, os.Stdout)
}
if err == nil {
return
}
responseError, ok := err.(files_sdk.ResponseError)
if ok && responseError.Type == "not-authorized/authentication-required" && profile.ValidSession() {
fmt.Fprintf(out[0], "Your session is invalid. Please login with:\n")
fmt.Fprintf(out[0], "\tfiles-cli login\n")
}
if ok && responseError.Type == "not-authorized/authentication-required" && profile.Config.GetAPIKey() != "" {
fmt.Fprintf(out[0], "Set the correct api-key with:\n")
fmt.Fprintf(out[0], "\tfiles-cli config set --api-key=\n")
}
if ok {
fmt.Fprintf(out[0], "%v\n", responseError)
} else {
fmt.Fprintf(out[0], "%v\n", err)
}
if ctx.Value("testing") != nil && !ctx.Value("testing").(bool) {
os.Exit(1)
}
}