OLD | NEW |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package deps | 5 package deps |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | |
9 "os" | 8 "os" |
10 | 9 |
11 "github.com/golang/protobuf/proto" | 10 "github.com/golang/protobuf/proto" |
12 "github.com/luci/luci-go/common/errors" | 11 "github.com/luci/luci-go/common/errors" |
13 "github.com/luci/luci-go/common/logging" | 12 "github.com/luci/luci-go/common/logging" |
14 dm "github.com/luci/luci-go/dm/api/service/v1" | 13 dm "github.com/luci/luci-go/dm/api/service/v1" |
15 "github.com/luci/luci-go/grpc/grpcutil" | 14 "github.com/luci/luci-go/grpc/grpcutil" |
16 "github.com/luci/luci-go/grpc/prpc" | 15 "github.com/luci/luci-go/grpc/prpc" |
17 "github.com/luci/luci-go/tumble" | 16 "github.com/luci/luci-go/tumble" |
18 "golang.org/x/net/context" | 17 "golang.org/x/net/context" |
19 "google.golang.org/grpc" | 18 "google.golang.org/grpc" |
20 "google.golang.org/grpc/codes" | 19 "google.golang.org/grpc/codes" |
21 ) | 20 ) |
22 | 21 |
23 const ek = logging.ErrorKey | 22 const ek = logging.ErrorKey |
24 | 23 |
25 type deps struct{} | 24 type deps struct{} |
26 | 25 |
27 var _ dm.DepsServer = (*deps)(nil) | 26 var _ dm.DepsServer = (*deps)(nil) |
28 | 27 |
29 func depsServerPrelude(c context.Context, methodName string, req proto.Message)
(context.Context, error) { | 28 func depsServerPrelude(c context.Context, methodName string, req proto.Message)
(context.Context, error) { |
30 // Many of the DM request messages can be Normalize'd. This checks them
for | 29 // Many of the DM request messages can be Normalize'd. This checks them
for |
31 // basic validity and normalizes cases where multiple representations ca
n mean | 30 // basic validity and normalizes cases where multiple representations ca
n mean |
32 // the same thing so that the service handlers only need to check for th
e | 31 // the same thing so that the service handlers only need to check for th
e |
33 // canonical representation. | 32 // canonical representation. |
34 if norm, ok := req.(interface { | 33 if norm, ok := req.(interface { |
35 Normalize() error | 34 Normalize() error |
36 }); ok { | 35 }); ok { |
37 if err := norm.Normalize(); err != nil { | 36 if err := norm.Normalize(); err != nil { |
38 » » » return nil, grpcAnnotate(err, codes.InvalidArgument).Rea
son("invalid request").Err() | 37 » » » return nil, grpcAnnotate(err, codes.InvalidArgument, "in
valid request").Err() |
39 } | 38 } |
40 } | 39 } |
41 return c, nil | 40 return c, nil |
42 } | 41 } |
43 | 42 |
44 const postludeDebugEnvvar = "DUMP_ALL_STACKS" | 43 const postludeDebugEnvvar = "DUMP_ALL_STACKS" |
45 | 44 |
46 var postludeOmitCodes = map[codes.Code]struct{}{ | 45 var postludeOmitCodes = map[codes.Code]struct{}{ |
47 codes.OK: {}, | 46 codes.OK: {}, |
48 codes.Unauthenticated: {}, | 47 codes.Unauthenticated: {}, |
49 codes.AlreadyExists: {}, | 48 codes.AlreadyExists: {}, |
50 codes.FailedPrecondition: {}, | 49 codes.FailedPrecondition: {}, |
51 codes.InvalidArgument: {}, | 50 codes.InvalidArgument: {}, |
52 codes.NotFound: {}, | 51 codes.NotFound: {}, |
53 codes.OutOfRange: {}, | 52 codes.OutOfRange: {}, |
54 codes.Aborted: {}, | 53 codes.Aborted: {}, |
55 } | 54 } |
56 | 55 |
57 func depsServerPostlude(c context.Context, methodName string, rsp proto.Message,
err error) error { | 56 func depsServerPostlude(c context.Context, methodName string, rsp proto.Message,
err error) error { |
58 retErr := grpcutil.ToGRPCErr(err) | 57 retErr := grpcutil.ToGRPCErr(err) |
59 if err != nil { | 58 if err != nil { |
60 code := codes.OK | 59 code := codes.OK |
61 _, printStack := os.LookupEnv(postludeDebugEnvvar) | 60 _, printStack := os.LookupEnv(postludeDebugEnvvar) |
62 if !printStack { | 61 if !printStack { |
63 code = grpc.Code(retErr) | 62 code = grpc.Code(retErr) |
64 _, omitStack := postludeOmitCodes[code] | 63 _, omitStack := postludeOmitCodes[code] |
65 printStack = !omitStack | 64 printStack = !omitStack |
66 } | 65 } |
67 if printStack { | 66 if printStack { |
68 » » » buf := &bytes.Buffer{} | 67 » » » errors.Log(c, err) |
69 » » » errors.RenderStack(err).DumpTo(buf) | |
70 » » » logging.Errorf(c, "%s", buf.String()) | |
71 } else { | 68 } else { |
72 logging.Infof(c, "returning gRPC code: %s", code) | 69 logging.Infof(c, "returning gRPC code: %s", code) |
73 } | 70 } |
74 } | 71 } |
75 return retErr | 72 return retErr |
76 } | 73 } |
77 | 74 |
78 func newDecoratedDeps() dm.DepsServer { | 75 func newDecoratedDeps() dm.DepsServer { |
79 return &dm.DecoratedDeps{ | 76 return &dm.DecoratedDeps{ |
80 Service: &deps{}, | 77 Service: &deps{}, |
(...skipping 12 matching lines...) Expand all Loading... |
93 // to codes.Internal. | 90 // to codes.Internal. |
94 func tumbleNow(c context.Context, m tumble.Mutation) error { | 91 func tumbleNow(c context.Context, m tumble.Mutation) error { |
95 err := tumble.RunMutation(c, m) | 92 err := tumble.RunMutation(c, m) |
96 if grpc.Code(err) == codes.Unknown { | 93 if grpc.Code(err) == codes.Unknown { |
97 logging.WithError(err).Errorf(c, "unknown error while applying m
utation %v", m) | 94 logging.WithError(err).Errorf(c, "unknown error while applying m
utation %v", m) |
98 err = grpcutil.Internal | 95 err = grpcutil.Internal |
99 } | 96 } |
100 logging.Fields{"root": m.Root(c)}.Infof(c, "tumbleNow success") | 97 logging.Fields{"root": m.Root(c)}.Infof(c, "tumbleNow success") |
101 return err | 98 return err |
102 } | 99 } |
OLD | NEW |