Skip to content

Commit d22dd24

Browse files
Fix #9 (#12)
1 parent 9bcb87c commit d22dd24

File tree

2 files changed

+45
-34
lines changed

2 files changed

+45
-34
lines changed

src/endpoint/context.rs

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -127,45 +127,56 @@ impl ContextInternal {
127127

128128
pub fn input<T: Deserialize>(&self) -> impl Future<Output = (T, InputMetadata)> {
129129
let mut inner_lock = must_lock!(self.inner);
130-
let input_result =
131-
inner_lock
132-
.vm
133-
.sys_input()
134-
.map_err(ErrorInner::VM)
135-
.and_then(|raw_input| {
136-
let headers = http::HeaderMap::<String>::try_from(
137-
&raw_input
138-
.headers
139-
.into_iter()
140-
.map(|h| (h.key.to_string(), h.value.to_string()))
141-
.collect::<HashMap<String, String>>(),
142-
)
143-
.map_err(|e| ErrorInner::Deserialization {
144-
syscall: "input_headers",
145-
err: e.into(),
146-
})?;
130+
let input_result = inner_lock
131+
.vm
132+
.sys_input()
133+
.map_err(ErrorInner::VM)
134+
.map(|raw_input| {
135+
let headers = http::HeaderMap::<String>::try_from(
136+
&raw_input
137+
.headers
138+
.into_iter()
139+
.map(|h| (h.key.to_string(), h.value.to_string()))
140+
.collect::<HashMap<String, String>>(),
141+
)
142+
.map_err(|e| {
143+
TerminalError::new_with_code(400, format!("Cannot decode headers: {e:?}"))
144+
})?;
147145

148-
Ok((
149-
T::deserialize(&mut (raw_input.input.into())).map_err(|e| {
150-
ErrorInner::Deserialization {
151-
syscall: "input",
152-
err: e.into(),
153-
}
154-
})?,
155-
InputMetadata {
156-
invocation_id: raw_input.invocation_id,
157-
random_seed: raw_input.random_seed,
158-
key: raw_input.key,
159-
headers,
160-
},
161-
))
162-
});
146+
Ok::<_, TerminalError>((
147+
T::deserialize(&mut (raw_input.input.into())).map_err(|e| {
148+
TerminalError::new_with_code(
149+
400,
150+
format!("Cannot decode input payload: {e:?}"),
151+
)
152+
})?,
153+
InputMetadata {
154+
invocation_id: raw_input.invocation_id,
155+
random_seed: raw_input.random_seed,
156+
key: raw_input.key,
157+
headers,
158+
},
159+
))
160+
});
163161

164162
match input_result {
165-
Ok(i) => {
163+
Ok(Ok(i)) => {
166164
drop(inner_lock);
167165
return Either::Left(ready(i));
168166
}
167+
Ok(Err(err)) => {
168+
let error_inner = ErrorInner::Deserialization {
169+
syscall: "input",
170+
err: err.0.clone().into(),
171+
};
172+
let _ = inner_lock
173+
.vm
174+
.sys_write_output(NonEmptyValue::Failure(err.into()));
175+
let _ = inner_lock.vm.sys_end();
176+
// This causes the trap, plus logs the error
177+
inner_lock.handler_state.mark_error(error_inner.into());
178+
drop(inner_lock);
179+
}
169180
Err(e) => {
170181
inner_lock.fail(e.into());
171182
drop(inner_lock);

src/endpoint/futures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ unsafe impl<T> Sync for TrapFuture<T> {}
2323
impl<T> Future for TrapFuture<T> {
2424
type Output = T;
2525

26-
fn poll(self: Pin<&mut Self>, ctx: &mut std::task::Context<'_>) -> Poll<T> {
26+
fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<T> {
2727
ctx.waker().wake_by_ref();
2828
Poll::Pending
2929
}

0 commit comments

Comments
 (0)