From 4ec320b9ab192a7e4ed53eae6fbc27daa9bc5b98 Mon Sep 17 00:00:00 2001 From: Samuel Gomez Date: Thu, 4 Jul 2024 19:51:32 +0100 Subject: [PATCH] Rewriting logic to be more clear --- integrationos-unified/src/unified.rs | 71 +++++++++++++++------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/integrationos-unified/src/unified.rs b/integrationos-unified/src/unified.rs index cdb242ac..4ea5bd67 100644 --- a/integrationos-unified/src/unified.rs +++ b/integrationos-unified/src/unified.rs @@ -24,7 +24,7 @@ use integrationos_domain::{ connection_model_schema::ConnectionModelSchema, database::DatabaseConfig, destination::{Action, Destination}, - environment::{self, Environment}, + environment::Environment, error::InternalError, get_secret_request::GetSecretRequest, hashed_secret::HashedSecret, @@ -37,7 +37,7 @@ use mongodb::{ options::{Collation, CollationStrength, FindOneOptions}, Client, }; -use serde_json::{json, Number, Value}; +use serde_json::{json, Map, Number, Value}; use std::{cell::RefCell, collections::HashMap, str::FromStr, sync::Arc}; use tracing::{debug, error}; @@ -677,39 +677,44 @@ impl UnifiedDestination { error!("Could not select body at response path {path}: {e}"); ApplicationError::bad_request(&e.to_string(), None) })?; - if !environment.is_production() - && matches!(config.action_name, CrudAction::GetMany | CrudAction::GetOne) - { - if bodies.is_empty() { - let error_string = format!( + + let is_returning_error = !environment.is_production() + && matches!(config.action_name, CrudAction::GetMany | CrudAction::GetOne); + let is_parseable_body = !bodies.is_empty() && !bodies.len() == 1; + + if bodies.is_empty() && is_returning_error { + let error_string = format!( "Could not map unified model. 3rd party Connection returned an invalid response. Expected model at path {path} but found none.", ); - let mut res = Response::builder() - .status(StatusCode::UNPROCESSABLE_ENTITY) - .body(json!({ - "message": error_string, - "passthrough": wrapped_body - })) - .map_err(|e| { - error!("Could not create response from builder for missing body"); - IntegrationOSError::from_err_code( - StatusCode::UNPROCESSABLE_ENTITY, - &e.to_string(), - None, - ) - })?; - *res.headers_mut() = headers; - return Ok(res); - } - if bodies.len() != 1 { - return Err(InternalError::invalid_argument( - &format!( - "Invalid number of selected bodies ({}) at response path {path}", - bodies.len() - ), - None, - )); - } + let mut res = Response::builder() + .status(StatusCode::UNPROCESSABLE_ENTITY) + .body(json!({ + "message": error_string, + "passthrough": wrapped_body + })) + .map_err(|e| { + error!("Could not create response from builder for missing body"); + IntegrationOSError::from_err_code( + StatusCode::UNPROCESSABLE_ENTITY, + &e.to_string(), + None, + ) + })?; + *res.headers_mut() = headers; + return Ok(res); + } + + if bodies.len() != 1 && is_returning_error { + return Err(InternalError::invalid_argument( + &format!( + "Invalid number of selected bodies ({}) at response path {path}", + bodies.len() + ), + None, + )); + } + + if is_parseable_body { Some(bodies.remove(0).clone()) } else { None