Skip to content

Commit

Permalink
Error if more than one handler is found in a component
Browse files Browse the repository at this point in the history
It's ambiguous which to run in that case so present an error instead of
picking an arbitrary one.

Signed-off-by: Alex Crichton <[email protected]>
  • Loading branch information
alexcrichton committed Mar 19, 2024
1 parent 5276b0f commit d8cf70a
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions crates/trigger-http/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{net::SocketAddr, str, str::FromStr};

use crate::{Body, HttpExecutor, HttpInstance, HttpTrigger, Store};
use anyhow::bail;
use anyhow::{anyhow, Context, Result};
use futures::TryFutureExt;
use http::{HeaderName, HeaderValue};
Expand Down Expand Up @@ -331,21 +330,36 @@ const WASI_HTTP_EXPORT_0_2_0: &str = "wasi:http/[email protected]";
impl HandlerType {
/// Determine the handler type from the exports of a component
pub fn from_component(ty: &ComponentType) -> Result<HandlerType> {
let mut handler_ty = None;

let mut set = |ty: HandlerType| {
if handler_ty.is_none() {
handler_ty = Some(ty);
Ok(())
} else {
Err(anyhow!(
"component exports multiple different handlers but \
it's expected to export only one"
))
}
};
for (name, _) in ty.exports() {
match name {
WASI_HTTP_EXPORT_2023_10_18 => return Ok(HandlerType::Wasi2023_10_18),
WASI_HTTP_EXPORT_2023_11_10 => return Ok(HandlerType::Wasi2023_11_10),
WASI_HTTP_EXPORT_0_2_0 => return Ok(HandlerType::Wasi0_2),
"fermyon:spin/inbound-http" => return Ok(HandlerType::Spin),
WASI_HTTP_EXPORT_2023_10_18 => set(HandlerType::Wasi2023_10_18)?,
WASI_HTTP_EXPORT_2023_11_10 => set(HandlerType::Wasi2023_11_10)?,
WASI_HTTP_EXPORT_0_2_0 => set(HandlerType::Wasi0_2)?,
"fermyon:spin/inbound-http" => set(HandlerType::Spin)?,
_ => {}
}
}

bail!(
"Expected component to either export `{WASI_HTTP_EXPORT_2023_10_18}`, \
`{WASI_HTTP_EXPORT_2023_11_10}`, `{WASI_HTTP_EXPORT_0_2_0}`, \
or `fermyon:spin/inbound-http` but it exported none of those"
)
handler_ty.ok_or_else(|| {
anyhow!(
"Expected component to either export `{WASI_HTTP_EXPORT_2023_10_18}`, \
`{WASI_HTTP_EXPORT_2023_11_10}`, `{WASI_HTTP_EXPORT_0_2_0}`, \
or `fermyon:spin/inbound-http` but it exported none of those"
)
})
}
}

Expand Down

0 comments on commit d8cf70a

Please sign in to comment.