Ejemplo n.º 1
0
func wrapMethodWithErrorMapper(srv *grpc.ServiceDesc, desc grpc.MethodDesc, mapper ErrorMapper) grpc.MethodDesc {
	h := desc.Handler
	desc.Handler = func(srv interface{}, ctx context.Context, dec func(interface{}) error, intc grpc.UnaryServerInterceptor) (out interface{}, err error) {
		res, err := h(srv, ctx, dec, intc)
		if err != nil {
			if e := mapper.HandleError(err); e != nil {
				err = e
			}
			return nil, err
		}
		return res, nil
	}
	return desc
}
Ejemplo n.º 2
0
func wrapMethodWithAuth(authDesc MethodAuthDesc, desc *grpc.MethodDesc, handler interface{}) {
	h := desc.Handler
	desc.Handler = func(srv interface{}, ctx context.Context, dec func(interface{}) error, intc grpc.UnaryServerInterceptor) (interface{}, error) {
		return h(srv, ctx, func(msg interface{}) error {
			if err := dec(msg); err != nil {
				return err
			}

			var (
				caller  = authDesc.GetCaller(msg)
				context interface{}
				err     error
			)

			if authDesc.Authenticate != nil {
				err = authDesc.Authenticate(handler, ctx, authDesc.Strategies, caller)
				if err != nil {
					return err
				}
				authDesc.SetCaller(msg, caller)
			}

			if authDesc.Authorize != nil {
				if authDesc.GetContext != nil {
					context = authDesc.GetContext(msg)
				}
				err = authDesc.Authorize(handler, ctx, authDesc.Scope, caller, context)
				if err != nil {
					return err
				}
			}

			return nil
		}, intc)
	}
}
Ejemplo n.º 3
-1
func wrapMethodWithTracer(srv *grpc.ServiceDesc, desc grpc.MethodDesc) grpc.MethodDesc {
	name := "/" + srv.ServiceName + "/" + desc.MethodName + "/root"
	h := desc.Handler
	desc.Handler = func(srv interface{}, ctx context.Context, dec func(interface{}) error, intc grpc.UnaryServerInterceptor) (out interface{}, err error) {
		span, ctx := trace.New(ctx, name, trace.WithPanicGuard)
		defer func(errPtr *error) {
			if span.Failed && *errPtr == nil {
				*errPtr = grpc.Errorf(codes.Internal, "internal server error")
			}
		}(&err)
		defer span.Close()

		return h(srv, ctx, dec, intc)
	}
	return desc
}