/
errors.go
46 lines (43 loc) · 1.04 KB
/
errors.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
package influxdb
import (
"github.com/influxdata/flux"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/internal/errors"
)
func handleError(target interface{}) error {
if errStr, ok := target.(string); ok {
return errors.New(codes.Unknown, errStr)
}
if internalErrMap, ok := target.(map[string]interface{}); ok {
internalErr := new(flux.Error)
if code, ok := internalErrMap["code"].(string); ok {
internalErr.Code = handleErrorCode(code)
}
if msg, ok := internalErrMap["message"].(string); ok {
internalErr.Msg = msg
}
if err, ok := internalErrMap["error"]; ok {
internalErr.Err = handleError(err)
}
return internalErr
}
return nil
}
func handleErrorCode(code string) codes.Code {
switch code {
case "internal error":
return codes.Internal
case "not found":
return codes.NotFound
case "invalid":
return codes.Invalid
case "unavailable":
return codes.Unavailable
case "forbidden":
return codes.PermissionDenied
case "unauthorized":
return codes.Unauthenticated
default:
return codes.Unknown
}
}