Add comments to server
-use rust_proto::plugin_sdk::generators::{
- generator_service_server::{
+use rust_proto::plugin_sdk::generators::{generator_service_server::{
GeneratorService,
GeneratorServiceServer,
- },
- GeneratedGraphProto,
- RunGeneratorRequestProto,
- RunGeneratorResponseProto,
-};
+}, GeneratedGraphProto, RunGeneratorRequestProto, RunGeneratorResponseProto, GeneratorsDeserializationError};
pub use rust_proto::{
graph_descriptions::GraphDescription,
plugin_sdk::generators::RunGeneratorRequest,
@@ -16,12 +11,18 @@ use tonic::transport::{
ServerTlsConfig,
};
+/// This trait is the main interface for Grapl Generator Plugins.
+/// Generators should be very straightforward - essentially they should just be parsers.
+/// Implementations of this trait should be passed into `exec_service`, which will
+/// serve the generator via gRPC.
pub trait GraphGenerator {
type Error: std::error::Error;
fn run_generator(&self, data: RunGeneratorRequest) -> Result<GraphDescription, Self::Error>;
}
+// This is only public so that it can show up in an `impl Into<GraphGeneratorImpl>` used in
+// a public interface
pub struct GraphGeneratorImpl<T>(T)
where
T: GraphGenerator + Send + Sync + 'static;
@@ -45,8 +46,18 @@ where
request: tonic::Request<RunGeneratorRequestProto>,
) -> Result<tonic::Response<RunGeneratorResponseProto>, tonic::Status> {
let request = request.into_inner();
- let request: RunGeneratorRequest = request.try_into().expect("todo");
- let graph_description = self.0.run_generator(request).expect("generator failed");
+ let request: RunGeneratorRequest = request.try_into()
+ .map_err(|e: GeneratorsDeserializationError| {
+ tracing::error!(message="Invalid RunGeneratorRequest", e=?e);
+ tonic::Status::invalid_argument(e.to_string())
+ })?;
+
+ let graph_description = self.0.run_generator(request)
+ // In the future we can ask implementations to give more information about their error
+ .map_err(|e| {
+ tracing::error!(message="Generator failed", e=?e);
+ tonic::Status::unknown(e.to_string())
+ })?;
Ok(tonic::Response::new(RunGeneratorResponseProto {
generated_graph: Some(GeneratedGraphProto {
@@ -79,7 +90,7 @@ where
.tls_config(ServerTlsConfig::new().identity(identity))?
.trace_fn(|request| {
tracing::info_span!(
- "PluginWorkQueue",
+ "exec_service",
headers = ?request.headers(),
method = ?request.method(),
uri = %request.uri(),
