-
Notifications
You must be signed in to change notification settings - Fork 9.6k
/
grpc_error.go
74 lines (66 loc) · 2.37 KB
/
grpc_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
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
72
73
74
package plugin
import (
"fmt"
"path"
"runtime"
"github.com/hashicorp/terraform/tfdiags"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// grpcErr extracts some known error types and formats them into better
// representations for core. This must only be called from plugin methods.
// Since we don't use RPC status errors for the plugin protocol, these do not
// contain any useful details, and we can return some text that at least
// indicates the plugin call and possible error condition.
func grpcErr(err error) (diags tfdiags.Diagnostics) {
if err == nil {
return
}
// extract the method name from the caller.
pc, _, _, ok := runtime.Caller(1)
if !ok {
logger.Error("unknown grpc call", "error", err)
return diags.Append(err)
}
f := runtime.FuncForPC(pc)
// Function names will contain the full import path. Take the last
// segment, which will let users know which method was being called.
_, requestName := path.Split(f.Name())
// Here we can at least correlate the error in the logs to a particular binary.
logger.Error(requestName, "error", err)
// TODO: while this expands the error codes into somewhat better messages,
// this still does not easily link the error to an actual user-recognizable
// plugin. The grpc plugin does not know its configured name, and the
// errors are in a list of diagnostics, making it hard for the caller to
// annotate the returned errors.
switch status.Code(err) {
case codes.Unavailable:
// This case is when the plugin has stopped running for some reason,
// and is usually the result of a crash.
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Plugin did not respond",
fmt.Sprintf("The plugin encountered an error, and failed to respond to the %s call. "
"The plugin logs may contain more details.", requestName),
))
case codes.Canceled:
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Request cancelled",
fmt.Sprintf("The %s request was cancelled.", requestName),
))
case codes.Unimplemented:
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Unsupported plugin method",
fmt.Sprintf("The %s method is not supported by this plugin.", requestName),
))
default:
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Plugin error",
fmt.Sprintf("The plugin returned an unexpected error from %s: %v", requestName, err),
))
}
return
}