Skip to content

Commit

Permalink
Rewriting logic to be more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
sagojez committed Jul 4, 2024
1 parent 4bba16d commit 4ec320b
Showing 1 changed file with 38 additions and 33 deletions.
71 changes: 38 additions & 33 deletions integrationos-unified/src/unified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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};

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4ec320b

Please sign in to comment.