Skip to content

feat(lambda-extension): make errors implement Debug+Display #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lambda-extension/src/extension.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::{logs::*, requests, Error, ExtensionError, LambdaEvent, NextEvent};
use hyper::{server::conn::AddrStream, Server};
use lambda_runtime_api_client::Client;
use std::{fmt, future::ready, future::Future, net::SocketAddr, path::PathBuf, pin::Pin, sync::Arc};
use std::{
convert::Infallible, fmt, future::ready, future::Future, net::SocketAddr, path::PathBuf, pin::Pin, sync::Arc,
};
use tokio::sync::Mutex;
use tokio_stream::StreamExt;
use tower::{service_fn, MakeService, Service};
Expand Down Expand Up @@ -45,14 +47,14 @@ impl<'a, E, L> Extension<'a, E, L>
where
E: Service<LambdaEvent>,
E::Future: Future<Output = Result<(), E::Error>>,
E::Error: Into<Box<dyn std::error::Error + Send + Sync>> + fmt::Display,
E::Error: Into<Box<dyn std::error::Error + Send + Sync>> + fmt::Display + fmt::Debug,

// Fixme: 'static bound might be too restrictive
L: MakeService<(), Vec<LambdaLog>, Response = ()> + Send + Sync + 'static,
L::Service: Service<Vec<LambdaLog>, Response = ()> + Send + Sync,
<L::Service as Service<Vec<LambdaLog>>>::Future: Send + 'a,
L::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
L::MakeError: Into<Box<dyn std::error::Error + Send + Sync>>,
L::Error: Into<Box<dyn std::error::Error + Send + Sync>> + fmt::Debug,
L::MakeError: Into<Box<dyn std::error::Error + Send + Sync>> + fmt::Debug,
L::Future: Send,
{
/// Create a new [`Extension`] with a given extension name
Expand Down Expand Up @@ -199,6 +201,7 @@ where

let res = ep.call(event).await;
if let Err(error) = res {
println!("{:?}", error);
let req = if is_invoke {
requests::init_error(extension_id, &error.to_string(), None)?
} else {
Expand Down Expand Up @@ -228,8 +231,8 @@ impl<T> Identity<T> {
}

impl<T> Service<T> for Identity<T> {
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>;
type Error = Infallible;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
type Response = ();

fn poll_ready(&mut self, _cx: &mut core::task::Context<'_>) -> core::task::Poll<Result<(), Self::Error>> {
Expand All @@ -251,7 +254,7 @@ impl<T> Service<()> for MakeIdentity<T>
where
T: Send + Sync + 'static,
{
type Error = Error;
type Error = Infallible;
type Response = Identity<T>;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

Expand Down
2 changes: 1 addition & 1 deletion lambda-extension/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub async fn run<E>(events_processor: E) -> Result<(), Error>
where
E: Service<LambdaEvent>,
E::Future: Future<Output = Result<(), E::Error>>,
E::Error: Into<Box<dyn std::error::Error + Send + Sync>> + fmt::Display,
E::Error: Into<Box<dyn std::error::Error + Send + Sync>> + fmt::Display + fmt::Debug,
{
let ext = Extension::new().with_events_processor(events_processor);
ext.run().await
Expand Down
9 changes: 6 additions & 3 deletions lambda-extension/src/logs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::{boxed::Box, sync::Arc};
use std::{boxed::Box, fmt, sync::Arc};
use tokio::sync::Mutex;
use tower::Service;
use tracing::{error, trace};
Expand Down Expand Up @@ -144,7 +144,7 @@ pub(crate) async fn log_wrapper<S>(
) -> Result<hyper::Response<hyper::Body>, Box<dyn std::error::Error + Send + Sync>>
where
S: Service<Vec<LambdaLog>, Response = ()>,
S::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
S::Error: Into<Box<dyn std::error::Error + Send + Sync>> + fmt::Debug,
S::Future: Send,
{
trace!("Received logs request");
Expand Down Expand Up @@ -172,7 +172,10 @@ where

{
let mut service = service.lock().await;
let _ = service.call(logs).await;
match service.call(logs).await {
Ok(_) => (),
Err(err) => println!("{:?}", err),
}
}

Ok(hyper::Response::new(hyper::Body::empty()))
Expand Down