From a90e666b11295320f2792d5dafc67632fea03ef5 Mon Sep 17 00:00:00 2001 From: Daniel Bloom <82895745+Daniel-Bloom-dfinity@users.noreply.github.com> Date: Fri, 29 Jul 2022 12:34:22 -0700 Subject: [PATCH] fix: `502` replica errors + map `DESTINATION_INVALID` to `404` (#43) This should map any `canister has no query method 'http_request'` or `canister not found` to `404`. Also change from `500` to `502` for replica errors. --- src/main.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 8dac1d2..0c69915 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,6 +70,12 @@ const MB: usize = 1024 * KB; const REQUEST_BODY_SIZE_LIMIT: usize = 10 * MB; const RESPONSE_BODY_SIZE_LIMIT: usize = 10 * MB; +/// https://internetcomputer.org/docs/current/references/ic-interface-spec#reject-codes +struct ReplicaErrorCodes; +impl ReplicaErrorCodes { + const DESTINATION_INVALID: u64 = 3; +} + /// Resolve overrides for [`reqwest::ClientBuilder::resolve()`] /// `ic0.app=[::1]:9090` pub(crate) struct OptResolve { @@ -270,11 +276,19 @@ async fn forward_request( match result { Ok((http_response,)) => Ok(http_response), + Err(AgentError::ReplicaError { + reject_code: ReplicaErrorCodes::DESTINATION_INVALID, + reject_message, + }) => Err(Ok(Response::builder() + .status(StatusCode::NOT_FOUND) + .body(reject_message.into()) + .unwrap())), + Err(AgentError::ReplicaError { reject_code, reject_message, }) => Err(Ok(Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) + .status(StatusCode::BAD_GATEWAY) .body(format!(r#"Replica Error ({}): "{}""#, reject_code, reject_message).into()) .unwrap())),