forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
45 lines (36 loc) · 884 Bytes
/
status.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
package opencensus
import (
"fmt"
microerr "github.com/micro/go-micro/errors"
"go.opencensus.io/trace"
"google.golang.org/genproto/googleapis/rpc/code"
)
var microCodeToStatusCode = map[int32]code.Code{
400: code.Code_INVALID_ARGUMENT,
401: code.Code_UNAUTHENTICATED,
403: code.Code_PERMISSION_DENIED,
404: code.Code_NOT_FOUND,
409: code.Code_ABORTED,
500: code.Code_INTERNAL,
}
func getResponseStatus(err error) trace.Status {
if err != nil {
microErr, ok := err.(*microerr.Error)
if ok {
statusCode := microErr.Code
code, ok := microCodeToStatusCode[microErr.Code]
if ok {
statusCode = int32(code)
}
return trace.Status{
Code: statusCode,
Message: fmt.Sprintf("%s: %s", microErr.Id, microErr.Detail),
}
}
return trace.Status{
Code: int32(code.Code_UNKNOWN),
Message: err.Error(),
}
}
return trace.Status{}
}